Why does DEBUG=False setting make my django Static Files Access fail?

后端 未结 14 1907
忘了有多久
忘了有多久 2020-11-22 05:16

Am building an app using Django as my workhorse. All has been well so far - specified db settings, configured static directories, urls, views etc. But trouble started sneaki

相关标签:
14条回答
  • 2020-11-22 05:41

    You can use WhiteNoise to serve static files in production.

    Install:

    pip install WhiteNoise==2.0.6
    

    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!

    Credit to Handlebar Creative Blog.

    BUT, it's really not recommended serving static files this way in production. Your production web server(like nginx) should take care of that.

    0 讨论(0)
  • 2020-11-22 05:45

    If you are using the static serve view in development, you have to have DEBUG = True :

    Warning

    This will only work if DEBUG is True.

    That's because this view is grossly inefficient and probably insecure. This is only intended for local development, and should never be used in production.

    Docs: serving static files in developent

    EDIT: You could add some urls just to test your 404 and 500 templates, just use the generic view direct_to_template in your urls.

    from django.views.generic.simple import direct_to_template
    
    urlpatterns = patterns('',
        ('^404testing/$', direct_to_template, {'template': '404.html'})
    )
    
    0 讨论(0)
  • 2020-11-22 05:45

    Just open your project urls.py, then find this if statement.

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

    You can change settings.DEBUG on True and it will work always. But if your project is a something serious then you should to think about other solutions mentioned above.

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

    In django 1.10 you can write so:

    urlpatterns += [ url(r'^media/(?P<path>.*)$', serve, { 'document_root': settings.MEDIA_ROOT, }), url(r'^static/(?P<path>.*)$', serve, { 'document_root': settings.STATIC_ROOT }), ]
    
    0 讨论(0)
  • 2020-11-22 05:45

    Support for string view arguments to url() is deprecated and will be removed in Django 1.10

    My solution is just small correction to 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')} ),
            )
    
    0 讨论(0)
  • 2020-11-22 05:55

    In urls.py I added this line:

    from django.views.static import serve 
    

    add those two urls in urlpatterns:

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

    and both static and media files were accesible when DEBUG=FALSE.
    Hope it helps :)

    0 讨论(0)
  • 2020-11-22 05:55

    You actually can serve static files in a production Django app, securely and without DEBUG=True.

    Rather than using Django itself, use dj_static in your WSGI file (github):

    # requirements.txt:
    
    ...
    dj-static==0.0.6
    
    
    # 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())
    
    0 讨论(0)
提交回复
热议问题