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博客

相关推荐
老胖闲聊3 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1183 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之4 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
lyaihao5 小时前
使用python实现奔跑的线条效果
python·绘图
ai大师5 小时前
(附代码及图示)Multi-Query 多查询策略详解
python·langchain·中转api·apikey·中转apikey·免费apikey·claude4
小小爬虾5 小时前
关于datetime获取时间的问题
python
蓝婷儿7 小时前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
chao_7897 小时前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表
大霞上仙8 小时前
nonlocal 与global关键字
开发语言·python
Mark_Aussie8 小时前
Flask-SQLAlchemy使用小结
python·flask