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

相关推荐
奇牙1 小时前
DeepSeek V4 Agent 开发实战:用 deepseek-v4-pro 搭建多步骤工作流(2026 完整代码)
python
斯维赤1 小时前
Python学习超简单第八弹:连接Mysql数据库
数据库·python·学习
qq_654366981 小时前
如何排查Oracle客户端连接慢_DNS解析超时与sqlnet配置优化
jvm·数据库·python
迷途酱2 小时前
手写一个 AI Agent:从 Function Calling 到自动化任务链
python
Gerardisite2 小时前
企微机器人开发指南
java·python·机器人·自动化·企业微信
城管不管3 小时前
嵌入模型Embedding Model
java·开发语言·python·embedding·嵌入模型
Architect_Lee3 小时前
python3.14.4环境搭建
python
适应规律3 小时前
pointnet 实战
python
慕涯AI3 小时前
Agent 30 课程开发指南 - 第28课
人工智能·python
迷途酱3 小时前
RAG 从零到一:用 Python 给大模型接上你的私有知识库
python