![](/img/trans.png)
[英]Django on Nginx serving static files but not media files when debug is False
[英]Django: serving static file on debug=false
我知道这个问题被多次询问过,但我发现和尝试的答案都没有帮助我。
这些是我的静态文件设置:
STATIC_ROOT = os.path.abspath(SETTINGS_PATH+'/staticfiles/')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/staticfiles/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(os.path.dirname(__file__), 'static'),
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
在myapp / urls.py中:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()
urlpatterns = patterns('',
# urls
)
urlpatterns += staticfiles_urlpatterns()
Collectstatic将所有文件复制到staticfiles /,因为它应该和我在所有静态文件上获得404。
我也在urls.py中尝试过这个:
if not settings.DEBUG:
urlpatterns += patterns('',
url(r'^staticfiles/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
)
这给了我以下类型的错误:
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/css/bootstrap.css". localhost:11
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/css/style.css". localhost:12
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/js/bootstrap.js". localhost:79
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/js/login.js".
我看不出我的设置有什么问题。 任何想法都是最受欢迎的。
正如您在文档中的警告框中看到的那样,在生产中(即使用debug=False
),您应该使用Web服务器来提供静态文件,而不是django。 因此,如果debug=False
, staticfiles
将拒绝为您的资产提供服务。
if DEBUG:
STATIC_ROOT = os.path.join(BASE_DIR, '/static')
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
并在urls.py中添加
import django.views.static
urlpatterns += [
url(r'^static/(?P<path>.*)$', django.views.static.serve, {'document_root': settings.STATIC_ROOT, 'show_indexes': settings.DEBUG})
]
我在我的root urls.py中使用下面的代码。 如果希望django开发服务器提供静态和媒体,则需要在设置文件中将FORCE_SERVE_STATIC设置为True。 并且不要忘记运行collectstatic命令来更新静态文件。
from django.conf.urls.static import static
from django.conf import settings
<...>
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
elif getattr(settings, 'FORCE_SERVE_STATIC', False):
settings.DEBUG = True
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(
settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.DEBUG = False
这是一个迟到的答案,但可能这将有助于某人。 有一个额外的选项可以与runserver
命令一起使用,它强制Django提供静态文件,即使在DEBUG=False
python manage.py runserver --insecure
Django docs ref: https : //docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#cmdoption-runserver-insecure
关闭调试后,Django将不再为您提供静态文件。 您的生产Web服务器(Apache或Heroku或Pythonanywhere或类似的东西)应该处理这个问题。
在您的urls.py文件中:
添加此行
from django.views.static import serve
在urlpatterns中添加这两个url:
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.