Why does the DEBUG = False setting fail django static file access?

I'm using Django as my primary building application.So far, everything has been fine - specifying database settings, static directories for configuration, URL s, views, and so on.But when I wanted to present my beautiful custom 404.html and 500.html pages, the trouble started to creep in.

I read the documentation on custom error handling, set the necessary configurations in UrlsConf, created the appropriate views, and added 404.html and 500.html to the template directory of my application (also specified in settings.py).

But the document says you can actually view custom error views until Debug is Off, you can actually view custom error views until Debug is Off, so I do close it to test my stuff, that's when things go crazy!

Not only can I not view the custom 404.html (in fact, it's loaded, but since each of my error pages contains graphic error messages (such as some beautiful images), the source of the error page is loaded, but nothing else is loaded!There is even no linked CSS or Javascript!

Normally, once I set DEBUG = False, all views will load, but no linked content (CSS, Javascript, images, etc.) will load!What happened?Is there something missing about static files and DEBUG settings?

#1st floor

In fact, you can safely provide static files in production Django applications without requiring DEBUG=True.

Instead of using Django itself, But In the WSGI file ( github ) used in dj_static :

# requirements.txt:


# YOURAPP/settings.py:

STATIC_ROOT = 'staticdir'
STATIC_URL = '/staticpath/'

# YOURAPP/wsgi.py:

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

#2nd floor

Support for string view parameters for url () is no longer supported and will be removed in Django 1.10.

My solution is only a minor modification to the Conrado solution above.

from django.conf import settings
import os
from django.views.static import serve as staticserve

if settings.DEBUG404:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', staticserve,
            {'document_root': os.path.join(os.path.dirname(__file__), 'static')} ),

#3rd floor

You can use WhiteNoise Provide static files in production.


pip install WhiteNoise

And change your wsgi.py file to this:

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

And you're good to go!

Thanks to Handlebar Creative Blog .

However, it is not actually recommended to provide static files in this way during production.Your production Web server (such as nginx) should be aware of this.

#4th floor

Just open your project urls.py and find the if statement.

if settings.DEBUG:
    urlpatterns += patterns(
        (r'^media/(?P<path>.*)','serve',{'document_root': settings.MEDIA_ROOT}), )

You can change the settings.Enable DEBUG on True and it will always run.However, if your project is serious, you should consider other solutions mentioned above.

if True:
    urlpatterns += patterns(
        (r'^media/(?P<path>.*)','serve',{'document_root': settings.MEDIA_ROOT}), )

In Django 1.10, you can write as follows:

urlpatterns += [ url(r'^media/(?P<path>.*)$', serve, { 'document_root': settings.MEDIA_ROOT, }), url(r'^static/(?P<path>.*)$', serve, { 'document_root': settings.STATIC_ROOT }), ]

#5th floor

Although it is not the safest, you can make changes in the source code.Navigate to Python/2.7/site-packages/django/conf/urls/static.py

Then edit as follows:

if settings.DEBUG or (prefix and '://' in prefix):

Therefore, if settings.debug==False, it will not affect the code, or you can try running static files using python manage.py runserver --runserver after run.

Note: Information only applies to tests

Tags: Django Javascript Python Database

Posted on Fri, 20 Mar 2020 21:16:04 -0700 by compphenom