首页 > 解决方案 > 如何在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>

但我想为不同的页面显示不同的标题,我不知道怎么做。有人知道吗?

标签: htmldjangodjango-templates

解决方案


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>

推荐阅读