正在开发中的 Django 站点:未为所有页面加载 CSS [英] Django site in developement: CSS not loading for all pages

查看:10
本文介绍了正在开发中的 Django 站点:未为所有页面加载 CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

恐怕我在这里不知所措,我查了一些类似的问题,但似乎没有一个适用.

I'm afraid I'm at a loss here, I checked out some similar questions but none of them seem to apply.

我在我的笔记本电脑上运行 django 开发服务器.我也用它来提供静态文件.我在静态根目录中有一个文件夹,其中包含两个名为 base.html 的模板的 CSS.

I am running the django developement server on my laptop. I use it to serve static files as well. I have a folder in the static root which contains the CSS for 2 templates both called base.html.

这是有效的头部部分:

<head>
    <title>| Entries | Latest entries</title>
    <link rel="stylesheet" type="text/css" href="/static/css/serenity.css" />
</head>

这是一个没有的:

<head>
    <title>Latest Photo Galleries</title>
    <link rel="stylesheet" type="text/css" href="/static/css/photologue.css" />
</head>

urls.py:

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

和静态网址:

    STATIC_URL = '/static/'

STATIC_ROOT 是绝对路径.

The STATIC_ROOT is an absolute path.

当我查看源代码并尝试打开指向 css 的链接时,我收到 404(未找到)

when I look at the source and try to open the link to the css I get a 404(not found)

为什么 Django 为一个应用程序提供 css 文件而不是另一个?(不工作的是 photologue 模板.)

why does Django serve the css file for one app but not the other? (the one not working is in the photologue template.)

任何帮助将不胜感激.

推荐答案

您使用 staticfiles 的方式有点不正确.虽然我不能确切地告诉你是什么导致了你目前的情况,但我可以告诉你,你的方法将来会让你头疼.不过,首先,我同意有关在 Django 服务器终端中查看请求流量的评论.查找 4xx 响应并确保请求的 URL 正确.这个:/Static/css/photologue/css 有两个错误.

The way you are using staticfiles is slightly incorrect. While I can't tell you exactly what is causing your current situation, I can tell you that your method will cause you headaches in the future. First things first though, I agree with the comments about watching your request traffic in the Django server terminal. Look for 4xx responses and make sure the requested URL is correct. This: /Static/css/photologue/css has two errors in it.

如果您不想进一步阅读,请删除 urls.py static.server 行并观察服务器终端.现在,这就是它的工作原理...

If you don't want to read further, drop the urls.py static.server line and watch the server terminal. Now, here's how it's all working...

您已经正确设置了设置变量,但您可能误解了STATIC_ROOT 的用途.STATIC_URL 是静态文件的完全限定或相对 URL.STATIC_ROOT 是最终保存所有静态文件的文件夹.它应该是空的.Django 负责通过 manage.py collectstatic 命令.这个想法是 Django 项目中的每个应用程序都有自己的 static/文件夹,其中包含它需要的 js/css/image 资产.此外,Django 将为 Admin 和您使用的任何其他第三方包收集静态资产.所有这些资产都将被组织到您的 STATIC_ROOT 文件夹中.假设您在收集之前拥有的文件会保留是不安全的.

You've got your settings variables correct but you may misunderstand the purpose of STATIC_ROOT. STATIC_URL is the fully qualified or relative URL for your static files. STATIC_ROOT is the folder that will ultimately hold all the static files. It should be empty. Django is responsible for filling it via the manage.py collectstatic command. The idea is each app in your Django project has its own static/ folder with the js/css/image assets that it needs. In addition, Django will collect the static assets for the Admin and any other third-party packages you use. All of these assets will be organized into your STATIC_ROOT folder. It's not safe to assume files you have their prior to collection will remain.

STATIC_ROOT = '/path/to/empty/static/folder/'  # or something dynamic with os.path methods
STATIC_URL = '/static/'

在您的情况下,您的 serenity 应用程序可能具有 serenity/static/css/serenity.css,而 photologue 具有 photologue/static/css/photologue.css.您可以将共享资源放在 base/static/ 文件夹中.

In your case maybe your serenity app has serenity/static/css/serenity.css and photologue has photologue/static/css/photologue.css. You could put shared assets in a base/static/ folder.

现在正确提供静态媒体.不要在 urls.py 中使用 'django.views.static.serve' 行.Django 的runserver自动提供静态文件.在幕后,它正在处理 collectstatic 行为并将所有静态资产收集在一起并为它们提供服务.在 Django 1.3 中使用这种类型的 URL 模式是不必要的,它会引起混乱,并且会错误地投入生产.请记住,您的网络服务器 (Apache/Nginx) 服务于静态资产.您的 Django urls.py 文件不需要了解有关它们的信息.

Now for properly serving static media. Do not use the 'django.views.static.serve' line in urls.py. Django's runserver will automatically serve static files. Behind the scenes it is handling the collectstatic behavior and gathering all your static assets together and serving them up. Using that type of URL pattern in Django 1.3 is unnecessary, a source of confusion, and the kind of thing that will mistakenly go to production. Remember, your webserver (Apache/Nginx) serves static assets. Your Django urls.py files don't need to know a thing about 'em.

引用模板中的静态 URL.您的模板中已经硬编码了 /static/ .这会起作用(但只是因为 STATIC_URL 是相同的值).为了更灵活,你有三个选项.

Referring to the static URL in templates. You've got /static/ hardcoded in your templates. That will work (but only because STATIC_URL is the same value). To be more flexible about it, you've got three options.

  1. 使用 href="{{ STATIC_URL }}css/photologue.css".只要您在 TEMPLATE_CONTEXT_PROCESSORS 中包含django.core.context_processors.static",该变量就会出现在您的模板中.
  2. 使用模板标签:{% load static %} ... href="{% get_static_prefix %}css/photologue.css"
  3. 在 Django 1.4 中,您将能够使用 {% load static from staticfiles %}... href="{% static 'css/photologue.css' %}"
  1. Use href="{{ STATIC_URL }}css/photologue.css". That variable will be in your templates as long as you include 'django.core.context_processors.static' in your TEMPLATE_CONTEXT_PROCESSORS.
  2. Use a templatetag: {% load static %} ... href="{% get_static_prefix %}css/photologue.css"
  3. In Django 1.4 you'll be able to use {% load static from staticfiles %}... href="{% static 'css/photologue.css' %}"

值得一读 Django 中的静态文件并了解 Django 1.4 中的变化

It's worth reading up on static files in Django and being aware of the changes coming in Django 1.4

这篇关于正在开发中的 Django 站点:未为所有页面加载 CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆