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

后端 未结 14 1920
忘了有多久
忘了有多久 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:45

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

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

    In django 1.10 you can write so:

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

提交回复
热议问题