一  首先创建一个django项目

django网页继承_django

1 urls.py文件

django网页继承_django_02

from django.contrib import admin
from django.urls import path
from hu import views
from django.conf.urls import url

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^list', views.list),

]

2 views.py文件

django网页继承_django_03

from django.shortcuts import render, redirect

def list(request):
    return render(request, 'child.html',)

3 father.html模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>将进酒</title>
</head>

    <style>
        body {
            margin: 0;
            padding-left: 100px;
            padding-right: 100px;
        }
        .poem-title {
            color: #AB3319;
            font-size: 50px;
            padding-top: 50px;

        }
        .poem-author {
            font-size: 30px;
            color: #5EA1F3;
            padding-top: 20px;
            font-weight: bold;
        }

        .poem-content {
            color: #444444;
            font-size: 20px;
            line-height: 36px;
            padding-top: 50px;
            padding-bottom: 50px;
        }

        .message-board {
            color: #62868D;
        }




    </style>
<body>

    <div class="poem-title">
    {% block poem_title %}诗歌-标题{% endblock poem_title %}
    </div>
    <div class="poem-author">
    {% block poem_author%}
    诗歌-作者
    {% endblock poem_author %}
    </div>

    <div class="poem-content">
    {% block poem_content%}
    诗歌-内容
    {% endblock poem_content %}
    </div>
    <div class="message-board">
    {% block message_board %}
    <div style="height: 300px; line-height: 300px; text-align: center; color: #BDBDBD; border: 1px solid #BDBDBD; border-radius: 6px;">留言区-暂时为空</div>
    {% endblock %}
    </div>

</body>

</html>

4 child.html文件

    {% extends "father.html" %}
    {% block poem_title %}将进酒{% endblock poem_title %}
    {% block poem_author %}
        李白
    {% endblock poem_author %}
    {% block poem_content %}
    君不见,黄河之水天上来,奔流到海不复回。<br>
    君不见,高堂明镜悲白发,朝如青丝暮成雪!<br>
    人生得意须尽欢,莫使金樽空对月。<br>
    天生我材必有用,千金散尽还复来。<br>
    烹羊宰牛且为乐,会须一饮三百杯。<br>
    岑夫子,丹丘生,将进酒,杯莫停。<br>
    与君歌一曲,请君为我倾耳听。<br>
    钟鼓馔玉不足贵,但愿长醉不复醒。<br>
    古来圣贤皆寂寞,惟有饮者留其名。<br>
    陈王昔时宴平乐,斗酒十千恣欢谑。<br>
    主人何为言少钱,径须沽取对君酌。<br>
    五花马、千金裘,呼儿将出换美酒,与尔同销万古愁!<br>
    {% endblock poem_content %}

5 最终效果

django网页继承_django_04