如何在 Django 中扩展到“base.html"模板样式 [英] How to extend to 'base.html' template styles in Django

查看:57
本文介绍了如何在 Django 中扩展到“base.html"模板样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用模板.我在 base.html 中设置了网站的主要部分,这些文件总是相同的(页眉、菜单、页脚......)在base.html"中正确编码,并带有链接到它的样式(在链接 rel="stylesheet") 中).

I work with templates in my application. I have the main part of the website styled in base.html, being the files that always will be the same (header, menu, footer...) properly coded in 'base.html' and also with the styles linked to it (in a link rel="stylesheet").

当我尝试按原样使用基本模板时,它运行良好,同时允许我在 de {% block content %} 之间添加内容并显示永久"部分(菜单、标题等),但上面没有样式 (CSS).我怎样才能扩展到这些样式表来加载 CSS 样式?

When I try to use the base template as it is, a base template, it works well while it lets me add content between de {% block content %} and also shows the 'permanent' parts (menu, header etc), but there have no style (CSS) on it. How could I also extend to these stylesheet to load the CSS styles??

将不胜感激,谢谢.

编辑 2: 这是我的 base.html 头部内容:

EDIT 2: Here's my base.html head content:

    <head>
<meta charset="utf-8">
{% load staticfiles %}
<title>{% block title %}Index{% endblock %}</title>
{% block style_base %}
<link href="{% static 'css/styles.css' %}" rel="stylesheet">
{% endblock %}
<meta name="description" content="{% block description %}{% endblock %}">
<link href='https://fonts.googleapis.com/css?family=Roboto+Mono:400,700,300' rel='stylesheet' type='text/css'>
<script src="static/myapp/jquery.min.js" type="text/javascript"></script>
<script src="static/myapp/main.js" type="text/javascript"></script>
</head>

这适用于 base.html,它获得正确的样式.但是,当尝试使用另一个模板在公共部分获取相同样式时,它不会获取样式.模板代码是这样开始的:

This works on base.html, it gets the correct styles. However, when trying to get the same styles in the common part with another template, it doesn't gets the styles. The template code starts like this:

{% extends "base.html" %}
{% load staticfiles %}
{% load i18n %}
{% include "base.html" %}
{% block title %}{% trans "Main index" %}{% endblock %}
{% block content1 %}

它从 base.html 获取所有正确的 HTML 但没有样式.我也尝试删除包含"标签或更改其位置,但没有结果.有什么问题?谢谢.

It gets all the correct HTML from base.html but unstyled. I also try delete the 'include' tag or changing its position but there's no result. What can be wrong? Thank you.

另外,控制台告诉我:

Not Found: /list/static/myapp/styles.css
[25/Mar/2016 01:03:03] "GET /list/static/myapp/styles.css HTTP/1.1" 404 3414

当我刷新页面(列表是我想从基础获取样式的模板所在的页面)时,它一直告诉我这个.List 不是我项目中的目录,但/static/myapp/styles.css 路径是正确的.会发生什么?

When I refresh the page (list is the page where there is the template I wanna get the styles from base) it keeps telling me this. List is not a directory in my project, but the /static/myapp/styles.css path is correct. What happens?

推荐答案

你必须调用 base.html 中的 CSS 文件,所以当你在其他页面中扩展 base.hml 时,CSS 将被调用.

You have to call the CSS files within the base.html, so when you extend the base.hml in your other pages, the CSS will be called.

对我来说,我通常这样做:

For me, I usually do this:

我有 Head.html,我将我在网站中使用的所有 Javascript 和 CSS 文件调用如下:

I have Head.html I call all Javascripts and CSS files I am using in the website inside it like this:

对于 CSS:

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

对于 JavaScript:

for javascript:

<script type="text/javascript" src="/static/js/example.js"></script>

然后,在网站的每个页面中,在标题标签之后,我像这样包含这个 Head.html:

then, in each page in the website, after the title tag I include this Head.html like this:

{% include "Head.html" %}

当您在每个页面中执行此操作时,CSS 和 javascripts 文件将在所有页面中可见并可调用.

When you do this in every page, the CSS and javascripts files will be seen in all the pages and callable.

另外,主要的 urls.py 文件应该是这样的:答案来自 这里"

Also, the main urls.py file should be like this: "the answer is from here"

urlpatterns = [ # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

这篇关于如何在 Django 中扩展到“base.html"模板样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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