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

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

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

最佳答案

在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:在debug = false上提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13801969/

相关文章:

python - 如何在python,django中将url解码为路径

python - 如何将多个可重复使用的Django应用程序绑定(bind)在一起?

python - 在 Django 1.4 中交叉引用外键

python - 按列对表进行排序并将其他列设置为顺序值以保持排序

django - 在管理区域外使用 django 文件管理器(或替代库)

python - 将从 url 获取的二进制数据保存到 Django FileField 中

django - 根据 "Two Scoops of Django"在 Django 中存储静态/模板文件的位置

css - Django:使用正确的 URL 从 S3 提供 CSS,但无法加载

python - django tutorial no module named staticfilespolls 错误

python - Django - 如何遍历字典列表以连接来自同一元素的值