Django 模版继承

1,设计母版页

Test/templates/6/base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- 修正了模板标签的全角字符问题 -->
    {% block title %}
    <title>这个是母版页</title>
    {% endblock %}

</head>
<body>

<table border="1" style="width: 1000px;">
    <tr>
        <td colspan="2" style="height: 30px; text-align: center;">
            这是Top区域,一般用于导航
        </td>
    </tr>

    <tr style="vertical-align: middle; height: 300px;">
        <td style="width: 200px;">
            这左边的的菜单
        </td>
        <td style="width: 500px;">
            <!-- 修正了模板标签的全角字符问题 -->
            {% block content %}
            这个区域随着内容页的变化而变化
            {% endblock %}
        </td>
    </tr>

    <tr>
        <td colspan="2" style="height: 30px; text-align: center;">
            这是版权区域
        </td>
    </tr>
</table>

</body>
</html>

2,设计内容页

Test/templates/6/welcome.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    {% extends "6\base.html" %}

    {% block title %}
    <title>这是欢迎页</title>
    {% endblock %}

</head>
<body>

    {% block content %}
    <div style="text-align: center;">
        欢迎来到我的小卖部!
    </div>
    {% endblock %}

</body>
</html>

3,添加视图函数

Test/app6/views.py

from django.shortcuts import render

# Create your views here.
def welcome(request):
    return render(request, '6\welcome.html')

4,添加路由地址

Test/app6/urls.py

from django.urls import path
from . import views


urlpatterns = [
    path('welcome', views.welcome, name='welcome'),

]

5,访问页面

http://127.0.0.1:8000/app6/welcome

Django 设计组件-CSDN博客

相关推荐
2401_858120264 小时前
探索sklearn文本向量化:从词袋到深度学习的转变
开发语言·python·机器学习
bigbearxyz5 小时前
Java实现图片的垂直方向拼接
java·windows·python
立秋67896 小时前
使用Python绘制堆积柱形图
开发语言·python
jOkerSdl6 小时前
第三十章 方法大全(Python)
python
小白学大数据6 小时前
HTML内容爬取:使用Objective-C进行网页数据提取
大数据·爬虫·python·html·objective-c·cocoa
逆境清醒7 小时前
开源数据科学平台Anaconda简介
人工智能·python·深度学习·机器学习·anaconda
SleepingBug7 小时前
[python][Anaconda]使用jupyter打开F盘或其他盘文件
开发语言·python·jupyter
mm_exploration7 小时前
torch常用函数
pytorch·python·深度学习
c实例7 小时前
爬虫 属性 方法
爬虫·python
爱煲汤的夏二7 小时前
CI脚本的python基础
java·python·ci/cd