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

Nea*_*ara 4 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',
) 
Run Code Online (Sandbox Code Playgroud)

在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()
Run Code Online (Sandbox Code Playgroud)

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}),
    )
Run Code Online (Sandbox Code Playgroud)

这给了我以下类型的错误:

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". 
Run Code Online (Sandbox Code Playgroud)

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

sec*_*ond 13

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

  • 我设置 debug=false 来重新创建我在 prod 服务器上遇到的问题。此应用程序部署在 Heroku 上。我在那里有同样的问题,但我使用 urls.py 的第二个版本。一切都奏效了一段时间,但没有了。我打算尽快使用 S3,但现在我需要它来工作 任何想法如何更改设置以使其工作? (2认同)

小智 12

在settings.py ::

if DEBUG: 
   STATIC_ROOT = os.path.join(BASE_DIR, '/static')
else:
   STATIC_ROOT = os.path.join(BASE_DIR, 'static') 
Run Code Online (Sandbox Code Playgroud)

并在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})
]
Run Code Online (Sandbox Code Playgroud)


sim*_*izz 6

我在根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
Run Code Online (Sandbox Code Playgroud)


Lal*_*ada 6

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

python manage.py runserver --insecure
Run Code Online (Sandbox Code Playgroud)

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

  • 对于那些只想开发 django 站点但让它运行得更快一点的人来说,这是正确的解决方案。这是正确的方法,因为它只是一个标志,并且可能/不应该转移到生产环境中。 (2认同)