【问题标题】:Django 404 error loading CSS from static directory从静态目录加载 CSS 的 Django 404 错误
【发布时间】:2012-10-02 16:57:01
【问题描述】:

我在从静态目录加载 CSS 时遇到问题:

settings.py

MEDIA_ROOT = '/he/sites/video1.hackedexistence.com/htdocs/media/'  
MEDIA_URL = '/media/'  

STATIC_ROOT = '/he/sites/video1.hackedexistence.com/htdocs/static/'  
STATIC_URL = '/static/'  

ADMIN_MEDIA_PREFIX = '/static/admin/'  

STATICFILES_DIRS = ()

url.py

from django.conf.urls.defaults import patterns, include, url  
from django.contrib import admin  
admin.autodiscover()

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),

    r'^beers/$', 'beer.views.BeersAll'),  
)

base.html 加载正常,css 无法加载

<link rel="stylesheet" type="text/css" href="/static/css/video1.css" />

css href 链接指向

Page not found (404)
    'css/video1.css' could not be found

css文件的完整路径:

/he/sites/video1.hackedexistence.com/htdocs/static/css/video1.css

我正在关注 Hacked Existence 的 youtube django 教程。而且他似乎继续进行此设置没有问题。我该如何纠正这个问题?

【问题讨论】:

  • 出于本教程的目的,我将 STATIC_ROOT 行清空,并在 STATICFILES_DIRS 中定义了路径。现在 CSS 正在工作。对于广泛的设置,我将进一步研究如下所述的路径修改和 apache 配置

标签: css django static


【解决方案1】:

Hacked Existence 可能已将他的 Web 服务器(基于您的上一个问题的 apache)配置为提供静态文件。

user1658078 是正确的,因为您需要以某种方式 serve the static files,并且在开发环境中您可以使用 django 的内置视图 django.contrib.staticfiles.views.serve(request, path) - 这个视图所做的只是查看在 STATICFILES_DIRS 中设置的路径以及每个应用程序内的static 子目录(例如,如果您的项目是我称为mysite 的应用程序,并且它有一个名为blog 的应用程序,那么它将查找mysite/blog/static/),并提供与该部分匹配的任何文件settings.STATIC_URL 值后面的 URL。

最后,值得指出的是,您的设置目前根本不起作用,因为您的 MEDIA_ROOT 和 STATIC_ROOT 目录设置为从 /he/sites/video1.hackedexistence.com/htdocs/ 的子目录提供服务,除非您创建了此目录,否则它将获胜根本不工作。

要修复静态文件、模板、管理文件和上传的文件,请按照以下说明操作:

在您的settings.py 中,将问题中的行替换为以下内容:

import os
PROJECT_DIR = os.path.dirname(__file__)

INSTALLED_APPS = (
    'django.contrib.staticfiles',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
#    'django.contrib.admindocs',
    'beer',  # note - I'm guessing the name of your application is beer
)

# django.contrib.staticfiles app collects files here when we run the collectstatic command
#  (depending on your web server config, you may want to change this to e.g. '/var/www/static/' when it comes to deployment)
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static-serve').replace('\\', '/')
# this should be prepended to your urls for static resources
STATIC_URL = '/static/'
# you can put static files which apply to your entire project here
STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, "static").replace('\\', '/'),
)

# the URL to where we have the admin media (static files) from a web browser's perspective (not needed if using Django 1.4 or greater)
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

# should be different from static files dir - this is where uploaded stuff goes
#  (depending on your web server config, you may want to change this to e.g. '/var/www/media/' when it comes to deployment)
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media').replace('\\', '/')
# if you need to serve the uploaded stuff again, you need to prefix your urls with this
MEDIA_URL = '/media/'

# you can put templates which apply to your entire project here
TEMPLATE_DIRS = (
    os.path.join(PROJECT_DIR, "templates").replace('\\', '/'),
)

还要确保将DEBUG 设置为True。

在 django 项目中的 urls.py 中(即 not 在 beer 的应用程序目录中),在末尾添加以下内容:

from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# serving of uploaded media + static files while debug mode is on
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)  # uploaded media
    urlpatterns += staticfiles_urlpatterns()  # files in each app's static/ dir

在您的 django 项目目录中创建以下目录:

  1. /media/ - 这是files uploaded by users 去的地方(例如,通过FileField 或ImageField)
  2. /static/ - 您可以在这里放置适用于整个 django 项目的静态文件(例如,您的整个页面的样式)。例如,使用此配置,如果您尝试访问位于 url static/css/video1.css 的 css 文件 video1.css,则确保该文件位于以下路径:/static/css/video1.css
  3. /templates/ - 这是您放置适用于整个 django 项目的模板的地方
  4. /beer/static/ - 这是您放置仅适用于一个特定站点的静态文件的地方。在此处构建文件的 url 时,您将它们视为在 /static/ 目录中,因此只需将 STATIC_URL 的值添加到相对文件名中。
  5. /beer/templates/ - 当你需要开始为你的视图创建模板时,你把你的模板放在这里(TEMPLATE_LOADERS 设置默认包括django.template.loaders.app_directories.Loader,它会在这个目录中找到模板)。与应用程序特定的静态目录类似,只需将此目录中的文件视为普通/templates/ 目录中的文件即可。
  6. /static-serve/ - 这在开发过程中不使用(所以现在不要太担心),但是当你最终想要部署你的 django 应用程序时,你运行 ./manage.py collectstatic,这将导致 django 复制每个文件中的所有文件应用程序目录的static 目录并将它们放在这里,然后您配置您的网络服务器以在 url 以 STATIC_URL 开头的值(在本例中为/static/)时从该目录提供文件。

现在您的静态文件将正确加载,管理员将正确显示,您的用户上传的文件将能够正确提供,并且您的模板将在您需要时正确找到。

【讨论】:

    【解决方案2】:

    我相信您需要提供静态文件。 Django 文档中描述了如何执行此操作的说明。 https://docs.djangoproject.com/en/dev/howto/static-files/ 请注意,开发和部署有不同的说明。

    【讨论】:

      猜你喜欢
      • 2020-12-30
      • 2015-11-08
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 2018-02-03
      • 2018-05-24
      相关资源
      最近更新 更多