How to configure static files in Django app for Heroku? - django

I deployed a django app to heroku, using "git push heroku master". It works fine but i have problem with static files. I can't configure it. What i have to do to get it started? Can you help me guys?
settings.py
DEBUG = False
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
base.html
<link rel='stylesheet' href='{% static "css/base.css" %}' />
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
from accounts.views import (login_view, register_view, logout_view)
from timetable.views import home
urlpatterns = [
url(r'^timetable/', include("timetable.urls", namespace='timetable')),
url(r'^admin/', admin.site.urls),
url(r'^home/', register_view, name='register'),
url(r'^login/', login_view, name='login'),
url(r'^logout/', logout_view, name='logout'),
url(r'^$', home, name='controler'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Try this
import os
from django.conf import settings
from django.conf.urls import include, patterns, url
from django.contrib import admin
# all other necessary imports
admin.autodiscover()
BASE_DIR = os.path.dirname((__file__))
urlpatterns = [
# all my url() patterns
]
if not settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
)

Related

Static Files not loading when DEBUG = False

I'm using namecheap shared hosting and I hosted my site using cpanel. My site is working fine but if i made DEBUG = False in project settings.py file the static files are not loading. My site url: https://drshahidabegum.com/
settings.py
-----------
# Static files (CSS, JavaScript, Images)
STATIC_DIR = [
BASE_DIR / "static",
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py in project folder
-------------------------
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dr_shahida.urls')),
]
# Configure Admin Titles
admin.site.site_header = "DR. Shahida Begum"
admin.site.site_title = "DR. Shahida Begum"
admin.site.index_title = "Welcome to Admin Dashboard"
handler404 = 'dr_shahida.views.error_404_view'
urls.py in app folder
-------
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.index, name='index'),
path('testimonial/', views.testimonial, name='testimonial'),
path('contact/', views.contact, name='contact'),
path('blog/', views.blog, name='blog'),
path('blog/post/<int:post_id>', views.blogdetailview, name='blogdetailview'),
path('set_language/<str:ln>', views.set_lang, name='set_lang'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
I want my static files to work when DEBUG = False in project settings.py file.

Refused to apply style because its MIME type ('text/html') in django and next js

hello i did everything with integration but nothing seems working,its again saying the same error
all next js files are default no changes
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'frontend/.next/static/css')
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
from django.contrib import admin
from django.urls import path,include
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.test)
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
pls help me find a solution to this problem i have done everything that i can do with this integration

django display uploaded by user images

I have ImageUploadField
I save images at my_project/forum_attachments directory.
But when I try to display them and see by this link: http://127.0.0.1:8000/forum_attachments/1466786056166112161_Nrns2WL.jpg
I get an error
Page not found (404) Request Method: GET Request
URL: http://127.0.0.1:8000/forum_attachments/1466786056166112161_Nrns2WL.jpg
What do I do?
UPD: urls.py:
urlpatterns = [
url(r'^polls/', include('some_app.urls')),
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^about/', views.about, name='about'),
url(r'^login/$', auth_views.login, name='login'),
url(r'^user_logout/$', views.user_logout, name='user_logout'),
url(r'^index_old/', views.index_old, name='index_old'),
url(r'^forum/', views.forum, name='forum'),
url(r'^vip/', views.vip, name='vip'),
url(r'^test/', views.test, name='test'),
url(r'^forum_new/', views.forum_new, name='forum_new'),
]
First set in settings.py:
MEDIA_URL = '/forum_attachments/'
Then in your main urls.py add following piece of code:
from django.conf.urls.static import static
from django.conf import settings
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In case you've not set your MEDIA_URL & MEDIA ROOT in settings or your local settings file, you should follow the below procedure.
Settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH,'../media/')
MEDIA_URL = '/media/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
STATIC_URL = '/static/'
Now in your Urls.py, do this:
from django.conf.urls.static import static
urlpatterns = [
url(r'^polls/', include('some_app.urls')),
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^about/', views.about, name='about'),
url(r'^login/$', auth_views.login, name='login'),
url(r'^user_logout/$', views.user_logout, name='user_logout'),
url(r'^index_old/', views.index_old, name='index_old'),
url(r'^forum/', views.forum, name='forum'),
url(r'^vip/', views.vip, name='vip'),
url(r'^test/', views.test, name='test'),
url(r'^forum_new/', views.forum_new, name='forum_new'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The 'static' was appended to the urlpatterns because your DEBUG might be set to True in your settings.py.

Django : TypeError: static() got an unexpected keyword argument 'document_root'

I am trying to view my uploaded image through web-browser/DRF-browsable API. So I added MEDIA_URL and MEDIA_ROOT to my setting.py file. When I trying to run the code , it showing TypeError: static() got an unexpected keyword argument 'document_root' . Here is my relevant portions of code,
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR,'Images')
MEDIA_URL = '/Images/'
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.templatetags.static import static
urlpatterns = [
# Examples:
# url(r'^$', 'project.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^$', index),
url(r'^health$', health),
url(r'^admin/', include(admin.site.urls)),
url(r'^sample/',sampelview),
url(r'myapp/',include(myRouter.urls)),
url(r'^test/$', testRequest),
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
This error is due to you are imported wrong References of static.Try to use from django.conf.urls.static import static instead of from django.templatetags.static import static

Serving static files in development mode

I've read the documentation of the Django about serving static files, and it says you should use django.conf.urls.static.static function in order to server them.
Here is my code:
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from settings import STATIC_ROOT, STATIC_URL
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^api/v1/', include('news.rest_urls', namespace='rest_framework')),
static(STATIC_URL, STATIC_ROOT)
]
And this is the error I receive:
'list' object has no attribute 'regex'
You need to define your STATIC_URL and STATIC_ROOT in settings.py and add to your urlpatterns next:
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)