亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Django 中的每個頁面上顯示單獨的標題

如何在 Django 中的每個頁面上顯示單獨的標題

絕地無雙 2023-12-19 16:32:41
我有一個 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>但我想為不同的頁面顯示不同的標題,但我不知道該怎么做。有人有什么想法嗎?
查看完整描述

4 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

基本.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)

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>


查看完整回答
反對 回復 2023-12-19
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

使用include而不是擴展base.html并將動態標題傳遞給base.html

django 鏈接:包含

{% include "base.html" with objects=website.title %}


查看完整回答
反對 回復 2023-12-19
?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

我根據我的知識給出這個答案:


為此創建一個文件: 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>

    {% include 'nav.html' %}


    {% block content %}

    {% endblock content %}


    {% include 'footer.html' %}


    {% include 'scripts.html' %}

</body>



</html>

我希望這對你有用。


查看完整回答
反對 回復 2023-12-19
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

使用可覆蓋的塊:


head.html


...

<title>{% block page_title %}{% endblock %}</title>

my_concrete_page.html


{% extends base.html %}


{% block page_title %}my concrete title{% endblock %}


查看完整回答
反對 回復 2023-12-19
  • 4 回答
  • 0 關注
  • 195 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號