如何在 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>

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


绝地无双
浏览 142回答 4
4回答

慕慕森

基本.html{% include 'head.html' with&nbsp; title=title %}<body>&nbsp; &nbsp; {% include 'nav.html' %}&nbsp; &nbsp; {% block content %}&nbsp; &nbsp; {% endblock content %}&nbsp; &nbsp; {% include 'footer.html' %}&nbsp; &nbsp; {% include 'scripts.html' %}</body></html>视图.pydef home(request):&nbsp; &nbsp; context = {&nbsp; &nbsp; &nbsp; &nbsp;"title":"Home"&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;return render(request,"template",context)head.html<head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <link rel="stylesheet" href="{% static 'css/materialize.css' %}">&nbsp; &nbsp; <link rel="stylesheet" href="{% static 'css/materialize.min.css' %}">&nbsp; &nbsp; <link rel="stylesheet" href="{% static 'css/style.css' %}">&nbsp; &nbsp; <link rel="stylesheet" href="{% static 'css/custom.css' %}">&nbsp; &nbsp; <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">&nbsp; &nbsp; <title>{{title}}</title></head>

开满天机

使用include而不是扩展base.html并将动态标题传递给base.htmldjango 链接:包含{%&nbsp;include&nbsp;"base.html"&nbsp;with&nbsp;objects=website.title&nbsp;%}

慕无忌1623718

我根据我的知识给出这个答案:为此创建一个文件: 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">为不同的标题制作不同的文件: 标题1.html<title>mytitle</title>标题2.html<title>mytitle</title>现在像这样添加到您的主文件中:<head>{% include 'head.html' %}{% include 'title1.html' %}</head><body>&nbsp; &nbsp; {% include 'nav.html' %}&nbsp; &nbsp; {% block content %}&nbsp; &nbsp; {% endblock content %}&nbsp; &nbsp; {% include 'footer.html' %}&nbsp; &nbsp; {% include 'scripts.html' %}</body></html>我希望这对你有用。

慕工程0101907

使用可覆盖的块:head.html...<title>{% block page_title %}{% endblock %}</title>my_concrete_page.html{% extends base.html %}{% block page_title %}my concrete title{% endblock %}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5