佛山网站域名过期,小学校园网站建设,成都专业网站搭建公司,做章的网站锋哥原创的Flask3 Python Web开发 Flask3视频教程#xff1a;
2025版 Flask3 Python web开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili
不管是开发网站还是后台管理系统#xff0c;我们页面里多多少少有公共的模块。比如博客网站#xff0c;就有公共的头部
2025版 Flask3 Python web开发 视频教程(无废话版) 玩命更新中~_哔哩哔哩_bilibili
不管是开发网站还是后台管理系统我们页面里多多少少有公共的模块。比如博客网站就有公共的头部菜单和底部栏。每个页面都有我们往往要抽取出这些公共模块然后统一维护。JinJa2提供了模板继承和include标签。 我们先把顶部信息和菜单抽取新建一个header.html放templates下
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
body
p头部菜单/p
/body
/html
同理 底部信息页抽取新建一个footer.html
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
body
p底部/p
/body
/html
我们再新建一个公共的父模版base.html其他子模板继承即可共同部分比如header,footer继承下来不同的部分自己实现。这里可以通过block标签在父类先预留一块。然后子类实现block即可。include标签引入公共模块。
!DOCTYPE html
html langen
headmeta charsetUTF-8title{% block title %}默认标题{% endblock %}/title
/head
body
{% include header.html %}
{% block content %}默认内容
{% endblock %}
{% include footer.html %}
/body
/html
新建博客首页index.html通过extends继承base.html以及重写实现block title和content
{% extends base.html %}
{% block title %}博客首页
{% endblock %}
{% block content %}博客列表
{% endblock %}
同理博客帖子页面detail.html
{% extends base.html %}
{% block title %}博客帖子
{% endblock %}
{% block content %}博客帖子
{% endblock %}
user.py里实现下视图函数
user_bp.route(/index)
def index():return render_template(index.html)user_bp.route(/detail)
def detail():return render_template(detail.html)
我们测试下