9

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

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

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

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

4

6 回答 6

15

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

于 2012-12-10T13:37:20.927 回答
14

在 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})
]
于 2014-05-07T16:53:40.900 回答
7

我在我的根 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
于 2015-05-03T17:10:04.127 回答
6

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

python manage.py runserver --insecure

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

于 2018-07-07T17:18:05.473 回答
1

在您的 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}),
于 2019-07-10T09:59:38.057 回答
0

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

于 2019-06-13T20:52:19.880 回答