【问题标题】:Django: serving static file on debug=falseDjango:在 debug=false 时提供静态文件
【发布时间】:2012-11-27 22:14:10
【问题描述】:

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

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

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

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

【问题讨论】:

  • 你得到的 mime 可能指的是一个 404 页面,用于根本没有提供的 css/脚本(因此你期望一个样式表,但你得到一个实际的 404 页面)。你对你的静态文件夹有权限吗?你是在 htaccess 下还是类似的?
  • 查看错误,您的静态请求似乎被重定向到登录页面。尝试在浏览器中浏览其中之一。是否有一些(其他受保护的)视图与 url 匹配?
  • ls -la 显示 drwxr-xr-x。在我的电脑上,我不在 htaccess 下,我不知道。在生产中,它在 Heroku 上。
  • @second 我确实添加了一个中间件来确保所有 url atm 的 LoginRequired,登录页面除外。但登录没有帮助。而且我在 debug=true 时提供静态文件没有任何问题。

标签: django django-staticfiles django-1.4


【解决方案1】:

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

【讨论】:

  • 我设置 debug=false 来重新创建我在 prod 服务器上遇到的问题。这个应用程序部署在 Heroku 上。我在那里也有同样的问题,但我使用的是 urls.py 的第二个版本。一切都工作了一段时间,但没有更多。我打算尽快使用 S3,但现在我需要它来工作。任何想法如何更改设置以使其正常工作?
  • +1 谢谢@second - 这似乎是我的问题。准备部署并在本地运行。另一个部署问题!
  • Angel 上面的其他答案是正确的还是解决方法?
  • 我不会在生产中使用 django 静态文件。如果您需要纯 python 解决方案(例如对于 heroku),请考虑 whitenoise
  • 对 Django 的小吐槽:我不想在生产中使用静态文件,我只想在没有调试错误页面等的情况下使用它进行开发。
【解决方案2】:

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

【讨论】:

  • 嘿,所以 django 人说,当调试为真时,原始方法是“这种方法效率极低,可能不安全,因此不适合生产。”在这里:docs.djangoproject.com/en/dev/howto/static-files/…。您正在做的事情是解决方法还是正确的做法?
  • 为什么会出现这个错误? Directory indexes are not allowed here
  • 对于使用 Django 版本 2.1 + STATIC_ROOT = 'static'的人
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    这是一个迟到的答案,但可能会对某人有所帮助。 runserver 命令有一个额外的选项,它强制 Django 提供静态文件,即使 DEBUG=False

    python manage.py runserver --insecure
    

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

    【讨论】:

    • 对于希望开发 django 站点但让它运行得更快一点的人来说,这是正确的解决方案。这也是正确的方法,因为它只是一个标志,可能/不应该转移到生产环境。
    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

      关闭调试后,Django 将不再为您提供静态文件。 您的生产网络服务器(Apache 或 Heroku 或 Pythonanywhere 或类似的东西)应该负责。

      【讨论】:

        猜你喜欢
        • 2019-06-01
        • 2020-10-27
        • 2019-12-02
        • 2016-11-26
        • 2018-08-24
        • 1970-01-01
        • 2015-09-12
        • 1970-01-01
        • 2022-12-10
        相关资源
        最近更新 更多