html - 如何在Django中的每个页面上显示单独的标题
问题描述
我有一个 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
只有 1 个标题。这是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>
但我想为不同的页面显示不同的标题,我不知道怎么做。有人知道吗?
解决方案
base.html
{% include 'head.html' with title=title %}
<body>
{% include 'nav.html' %}
{% block content %}
{% endblock content %}
{% include 'footer.html' %}
{% include 'scripts.html' %}
</body>
</html>
视图.py
def home(request):
context = {
"title":"Home"
}
return render(request,"template",context)
头.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>
推荐阅读
- laravel - 当我在 laravel 中提交表单时,我将如何在隐藏字段中获取当前页面标题
- regex - 如何在 db2 中拆分字符串?
- r - 执行矩阵乘法的正确方法
- php - Laravel 刀片中不显示会话值
- r - 如何使用混合分布进行生存分析
- mongodb - 为什么服务器没有为我的 'mongoDB' 启动?
- php - 我想将 CSRF 令牌添加到登录页面上发出的每个帖子请求
- javascript - 如何使用 javascript 检查应用程序是否已安装
- php - Codeigniter:当用户登录时仅调用一次控制器以显示通知
- python-3.x - 将 GlobalMaxPooling1D 池化层的输出映射回 keras 中的输入位置