Django:在debug = false上提供静态文件

时间:2012-12-10 13:31:10

标签: django django-staticfiles django-1.4

我知道这个问题被多次询问过,但我找到并试过的答案都没有帮助我。

这些是我的静态文件设置:

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". 

我看不出我的设置有什么问题。任何想法都是最受欢迎的。

6 个答案:

答案 0 :(得分:13)

正如您在the docs中的警告框中看到的那样,在生产中(即使用debug=False),您应该使用Web服务器来提供静态文件,而不是django。因此,如果staticfilesdebug=False将拒绝为您的资产提供服务。

答案 1 :(得分:12)

settings.py :: 中的

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})
]

答案 2 :(得分:5)

我在我的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

答案 3 :(得分:2)

这是一个较晚的答案,但可能会帮助某人。 runserver命令还有一个额外的选项,可强制Django即使在DEBUG=False

下也可以提供静态文件
python manage.py runserver --insecure

Django文档参考:https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#cmdoption-runserver-insecure

答案 4 :(得分:0)

关闭调试功能后,Django将不再为您提供静态文件。 您的生产Web服务器(Apache或Heroku或Pythonanywhere或类似的东西)应该解决这个问题。

答案 5 :(得分:0)

在您的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}),