我有一个Django网站,将html文件分成base.html
文件,如下所示:
{% include 'head.html' %}
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock content %}
{% include 'footer.html' %}
{% include 'scripts.html' %}
</body>
</html>
由于包含head.html
,每个页面上的标题都是相同的,因为head.html
只有一个标题。这是head.html
文件:
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'css/materialize.css' %}">
<link rel="stylesheet" href="{% static 'css/materialize.min.css' %}">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<title>mytitle</title>
</head>
但是我想为不同的页面显示不同的标题,我不知道该怎么做。有人知道吗?
答案 0 :(得分:1)
使用 include 代替base.html的扩展名,并将动态标题传递给base.html
django链接:include
{% include "base.html" with objects=website.title %}
答案 1 :(得分:1)
使用块,它们是overridable:
head.html
...
<title>{% block page_title %}{% endblock %}</title>
my_concrete_page.html
{% extends base.html %}
{% block page_title %}my concrete title{% endblock %}
答案 2 :(得分:1)
我是根据我的知识给出这个答案的:
为此制作一个文件: head.html
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'css/materialize.css' %}">
<link rel="stylesheet" href="{% static 'css/materialize.min.css' %}">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
为您的不同标题创建不同的文件: title1.html
<title>mytitle</title>
title2.html
<title>mytitle</title>
现在像这样添加您的主文件:
<head>
{% include 'head.html' %}
{% include 'title1.html' %}
</head>
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock content %}
{% include 'footer.html' %}
{% include 'scripts.html' %}
</body>
</html>
我希望这对您有用。
答案 3 :(得分:1)
base.html
{% include 'head.html' with title=title %}
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock content %}
{% include 'footer.html' %}
{% include 'scripts.html' %}
</body>
</html>
views.py
def home(request):
context = {
"title":"Home"
}
return render(request,"template",context)
head.html
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'css/materialize.css' %}">
<link rel="stylesheet" href="{% static 'css/materialize.min.css' %}">
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<title>{{title}}</title>
</head>
答案 4 :(得分:0)
我不得不结合@Ivan和@Soham的想法。我从head.html
中删除了标题标签,并将其添加到base.html
中。除此之外,我还在title标签内使用了可覆盖的block标签。所以现在我的base.html
看起来像这样:
<!DOCTYPE html>
<html lang="en">
{% include 'head.html' %}
<title>{% block title %}{% endblock title %}</title>
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock content %}
{% include 'footer.html' %}
{% include 'scripts.html' %}
</body>
</html>
我现在要做的就是在其他页面中相应地使用标签:
{% extends 'base.html' %}
{% block title %}whatever i want the title to be{% endblock title %}