Django 模板扩展和 CSS
我有这样的基本模板:
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="/static/style.css"/>
当我刷新页面时,此文本会记录在日志中:
[01/Dec/2011 18:22:00] "GET /search/ HTTP/1.1" 200 2760
[01/Dec/2011 18:22:00] "GET /static/style.css HTTP/1.1" 200 54
所以,这意味着 CSS 从服务器正确加载。但这不起作用!如果我将此 CSS 作为文本直接包含到基本模板中,它就可以正常工作。 静态文件配置没问题。
I have such base template:
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="/static/style.css"/>
And this text in logs when I refresh the page:
[01/Dec/2011 18:22:00] "GET /search/ HTTP/1.1" 200 2760
[01/Dec/2011 18:22:00] "GET /static/style.css HTTP/1.1" 200 54
So, it means that CSS loads from server correctly. But it doesn't work! If I include this CSS as text directly into the base template, it works properly.
The static files configuration is OK.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否将 css 放在一个块中,并将其放在底座的头部?
在您的浏览器中,页面源代码看起来如何?样式表在头部吗?你能点击链接并查看实际的CSS吗?
Are you putting the css in a block that will put it in the head of the base?
In your browser, how does the page source look? Is the style sheet in the head? can you click the link and see the actual css?
这是一个非常晚的回复,但现在可能比五年前更相关:确保您正在编辑的样式表不是由托管您网站的服务器生成的。
我不完全清楚 @RankoR 是否对 style.css 文件进行了更改,并且它们没有反映在网站上,或者是否将零样式应用于模板,但如果您遇到前者:您可以通过将您自己的 .css 文件夹添加到根文件夹,并向其中添加您想要更改的定义,快速排除它在您和最终渲染状态之间生成的可能性。
假设您正在尝试自定义导航栏的样式 - 如果您想更改
.navbar-inverse {background-color: #222;边框颜色:#090909 }
到.navbar-inverse {背景颜色:#222;边框颜色:#090909; font:20px 粗体 Arial, sans-serif }
,添加到新的空白 .css 文件.navbar-inverse2 {background-color: #222;边框颜色:#090909; font:20px 粗体 Arial, sans-serif }
,并将 html 中的.navbar-inverse
标记更改为navbar-inverse2
。如果您只是将样式表链接到中的原始样式表下,并且会呈现该更改:您就在前往 Solution Town 的路上了!
This an extremely late response, but possibly more relevant now than it would have been five years ago: Make sure the stylesheet you're editing isn't being generated by the server hosting your site.
I'm not entirely clear on whether @RankoR was making changes to the style.css file, and they weren't being reflected on the site, or if zero styles were being applied to the template whatsoever, but if you run into the former: You can quickly rule out the possibility that it's being being generated somewhere between you and its final, rendered state by adding your own .css folder to the root folder, and adding a definition to it that you're trying to change.
Let's say you're trying to customize your navbar's styling -- if you're wanting to change
.navbar-inverse {background-color: #222; border-color: #090909 }
to.navbar-inverse {background-color: #222; border-color: #090909; font:20px bold Arial, sans-serif }
, add to your new, blank .css file.navbar-inverse2 {background-color: #222; border-color: #090909; font:20px bold Arial, sans-serif }
, and change the.navbar-inverse
tag(s) in your html tonavbar-inverse2
. If you just link your stylesheet under the original one in the<head>
, and that change is rendered: You're on your way to Solution Town!你说的“不起作用”是什么意思?是否没有进行样式设置、图像是否丢失等?您确定已保存对 CSS 文件的更改吗?似乎在 /static/style.css 找到了一个文件,所以推测那里有一些东西......
What do you mean by "doesn't work"? Does no styling take place, are images missing, etc.? Are you sure you saved your changes to the CSS file? It seems a file was found at /static/style.css, so presumably there is something there...
你使用STATIC_URL还是MEDIA_URL来访问css?如果是,您是否将 django.contrib.contenttypes 添加到设置中已安装的应用程序中?另请检查设置中的 STATICFILES_DIRS 和 STATIC_ROOT/MEDIA_ROOT。有关详细信息,请参阅 https://docs.djangoproject.com/en/1.3/参考/contrib/staticfiles/ 。
另外,如果您使用 MEDIA_URL,您可以尝试在 url 中提供媒体根,例如:
Do u use STATIC_URL or MEDIA_URL to access css? If you are, did u add django.contrib.contenttypes to installed apps in settings? Also check STATICFILES_DIRS and STATIC_ROOT/MEDIA_ROOT in settings. For details look through https://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/ .
Also if you use MEDIA_URL you may try to serve media root in urls, somehow like: