How to run static file when debug is false? [closed]

有些话、适合烂在心里 提交于 2021-02-13 17:39:27

问题


With debug turned off Django won't handle static files for you any more - your production web server.

Implement this code inside your project:

settings.py

STATIC_DIR=os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'

if DEBUG:
    STATICFILES_DIRS = [
        STATIC_DIR,
    ]
else:
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'

urls.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
     ...
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,document_root=settings.STATICFILES_DIRS)

来源:https://stackoverflow.com/questions/66132733/how-to-run-static-file-when-debug-is-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!