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

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

问题描述

我怕我在这里亏了,我查了一些类似的问题,但似乎都没有适用。



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



这是一个工作的头部:

 < head> 
< title> |条目|最新条目< / title>
< link rel =stylesheettype =text / csshref =/ static / css / serenity.css/>
< / head>

这里是没有的:

 < HEAD> 
< title>最新图片库< / title>
< link rel =stylesheettype =text / csshref =/ 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是绝对路径。 >

当我看看源码并尝试打开链接到css我得到一个404(未找到)



为什么Django为一个应用程序提供css文件,但不是其他应用程序?
(一个不工作的是photologue模板。)



任何帮助将不胜感激。

解决方案

使用 staticfiles 的方式稍微不正确。虽然我不能准确地告诉你什么是导致你目前的情况,我可以告诉你,你的方法将会导致你头痛的将来。首先,我同意关于在Django服务器终端上观看您的请求流量的评论。寻找4xx回复,并确保所请求的URL是正确的。这个: / Static / css / photologue / css 有两个错误。



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



你的设置变量是正确的,但你可能会误会STATIC_ROOT 的目的。 STATIC_URL是静态文件的完全限定或相对URL。 STATIC_ROOT是最终将保存所有静态文件的文件夹。应该是空的。 Django负责通过 manage.py collectstatic 命令。这个想法是Django项目中的每个应用程序都有自己的静态/文件夹,需要它的js / css / image资源。此外,Django将收集管理员和您使用的任何其他第三方软件包的静态资产。所有这些资产将被组织到您的STATIC_ROOT文件夹中。

  STATIC_ROOT ='/ path / to / empty / static不安全地假设您收集的文件将保留。 /文件夹/'#或动态的os.path方法
STATIC_URL ='/ static /'

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



现在,正确投放静态媒体即可。不要在 urls.py 中使用'django.views.static.serve'行。 Django的 runserver 将自动提供静态文件。在幕后,它正在处理收集行为,并将所有静态资产收集在一起并提供服务。在Django 1.3中使用这种类型的URL模式是不必要的,一个混乱的来源,以及那些会错误地进行生产的东西。请记住,您的网络服务器(Apache / Nginx)提供静态资产。您的Django urls.py文件不需要知道有关em的内容。



参考模板中的静态网址。您的模板中已经有 / static / 硬编码。这将工作(但只因为STATIC_URL是相同的值)。为了更加灵活,您可以三个选项


  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'%}

值得在Django中静态读取文件,并注意Django 1.4中的变化。


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

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.

this is the head section of the one that works:

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

here is the one which does not:

<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}),

and the static URL:

    STATIC_URL = '/static/'

The STATIC_ROOT is an absolute path.

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

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

Any help would be appreciated.

解决方案

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.

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...

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/'

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.

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.

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. 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' %}"

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天全站免登陆