程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django production environment cant load static files. How to deal with the problem

編輯:Python

background :

Online deployment Django The project has been set settings.DEBUG = False It will cause static files to be 404 The situation of . The main reason is that it should be closed DEBUG After the model ,Django There is no static file service .

runserver Start of

If the operation is through runserver The way of command , That's easy , Start up runserver Add --insecure Options can force django Working with static files .

Start in other ways

But if it's through uwsgi or daphne When it starts , The way of adding options doesn't work . To solve this problem , We need to use the static file service manually , This method is recommended , Because it also supports runserver The way .

resolvent 1:

from django.urls import path, re_path, include
from django.views.static import serve
from django.conf import settings
urlpatterns += [
# Static file access method I :
re_path(r'^media/(?P<path>.*)$', serve, {"document_root": settings.MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT, }),
]

resolvent 2:

# Routing method 2 for accessing pictures and static resources
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.STATIC_ROOT)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved