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

后端 未结 14 2072
忘了有多久
忘了有多久 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 06:02

    I agree with Marek Sapkota answer; But you can still use django URFConf to reallocate the url, if static file is requested.

    Step 1: Define a STATIC_ROOT path in settings.py

    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    

    Step 2: Then collect the static files

    $ python manage.py collectstatic
    

    Step 3: Now define your URLConf that if static is in the beginning of url, access files from the static folder staticfiles. NOTE: This is your project's urls.py file:

    from django.urls import re_path
    from django.views.static import serve
    
    urlpattern += [
      re_path(r'^static/(?:.*)$', serve, {'document_root': settings.STATIC_ROOT, })
    ]
    

提交回复
热议问题