【问题标题】:Static files status' 404静态文件状态'404
【发布时间】:2014-08-28 22:24:55
【问题描述】:

我正在使用 heroku 来托管我的 Django(1.6) 应用程序(称为“Zen”)。问题是没有显示静态文件。换句话说,我的应用程序中没有 CSS 和 JS,因为它没有找到这些文件。我在这里查看了其他问题,并将我的应用配置如下:

Settings.py

##### Static asset configuration #####
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

urls.py:

from django.conf.urls.static import static
from zen import settings

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)

urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )

wsgi.py

import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zen.settings")
application = Cling(get_wsgi_application())

显然,在使用git push heroku master 推送我的应用程序后,一切正常:

-----> Preparing static assets Running collectstatic... 69 static files copied to '/app/staticfiles'.

但正如您所见,我的应用程序中没有 CSS (http://obscure-reef-8874.herokuapp.com/)。我查看了我的应用程序的日志,大多数 css 和 js 文件都处于 404 状态...我尝试了所有方法,你能帮帮我吗?

编辑:

我没有找到错误,我重新创建了一个应用程序,它可以工作,很简单。

【问题讨论】:

  • 试试STATIC_ROOT = os.path.join(PROJECT_PATH, 'staticfiles')

标签: django heroku


【解决方案1】:

设置

STATIC_ROOT = 'static' 

试试 可能是STATIC_ROOT和STATICFILES_DIRS不同造成的

【讨论】:

    【解决方案2】:

    问题不在于您的静态文件服务:这很好,如果您转到管理应用程序:http://obscure-reef-8874.herokuapp.com/admin/,您会看到。 /static/admin/ 下的文件没有问题。

    如果没有看到应用程序的结构以及在模板中输出静态链接的方式,这很难提供帮助,我猜您的文件位于 /static/ 的子目录中,因此您需要引用该子目录在模板中资产的链接中。

    请注意,您应该从 urls.py 中删除额外的静态 URL 模式。当 DEBUG 为 False 时,它​​们不起作用。您的文件由 Django 外部的 Cling 提供。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-30
      • 2020-10-17
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 2017-06-05
      • 2018-05-24
      • 1970-01-01
      相关资源
      最近更新 更多