Created
November 10, 2021 04:44
-
-
Save joeypy/bb49345db565071b01d65076e3efafbf to your computer and use it in GitHub Desktop.
Django static files not working when debug false || debug true
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In this article you will learn how to fix problem of not loading static files | |
# and media in Django even the DEBUG is FALSE. This is the easiest and safest solution. | |
# Django static and media files not working when debug is false | |
# Problem: | |
DEBUG = False #True | |
ALLOWED_HOSTS = ['*'] #Host name | |
# Problem Fix: | |
# Let's see, How you can fix the problem of Django static and media files not working when DEBUB = False : | |
# 1.)First way: devserver in insecure mode | |
# If you still need to server static locally (e.g. for testing without debug) you can run devserver in insecure mode: | |
# python manage.py runserver --insecure | |
# --insecure: it means you can run server without security mode. | |
# Note: This is Exactly you must type on terminal to run your project without "DEBUG = TRUE" and | |
# then you see all assets (static) file is loading correctly On local server. | |
# 2.)Second way: devserver in secure mode | |
# Step 1 : Define a STATIC_ROOT and MEDIA_ROOT path in settings.py. | |
# ➤ Code: settings.py | |
STATIC_URL = '/static/' | |
MEDIA_URL = '/media/' | |
if DEBUG: | |
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] | |
else: | |
STATIC_ROOT = os.path.join(BASE_DIR, 'static') | |
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') | |
# Step 2 : Now define your URLConf that if static is in the beginning of url, access files from the static folder staticfiles. | |
#------------------------------------------------------------------------------------------------------------------------------ | |
# ➤ Code: urls.py | |
from django.conf.urls import url | |
from django.conf import settings | |
from django.views.static import serve | |
urlpatterns = [ | |
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}), | |
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}), | |
] | |
# Note: This is your project's urls.py file. | |
#------------------------------------------------------------------------------------------------------------------------------ | |
# Step 3 : Now you can run your django project. | |
# python manage.py runserver | |
# Note : Congratulations, now static and media files will be loaded in your Django project when DEBGU = False. Have fun! |
how does this urlpatter work? it didnot work for me
you can use 're_path' instead of 'url'
from django.urls import path, include, re_path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
Thanks man its life saving code for me ........after implementation i going to live my website .....thanks a lot
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how does this urlpatter work? it didnot work for me