【发布时间】: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