《每天5分钟用Flask搭建一个管理系统》第4章:模板渲染

第4章:模板渲染

4.1 模板的概念和使用

模板是一种用于生成输出的方法,它允许您将Python代码和HTML标记混合在一起,从而创建动态网页。

示例代码:基本模板

html 复制代码
<!-- templates/home.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>
    <h1>{{ header }}</h1>
    <p>{{ content }}</p>
</body>
</html>

在Flask中,您可以使用render_template函数来渲染模板。

示例代码:渲染模板

python 复制代码
from flask import render_template

@app.route('/')
def home():
    title = "Home Page"
    header = "Welcome to My Website"
    content = "This is the home page."
    return render_template('home.html', title=title, header=header, content=content)
4.2 Jinja2模板引擎介绍

Jinja2是Flask的默认模板引擎,它提供了强大的模板语言功能。

示例代码:使用Jinja2模板语法

html 复制代码
<!-- templates/about.html -->
{% if user %}
    <p>Welcome, {{ user.username }}!</p>
{% else %}
    <p>Welcome, guest!</p>
{% endif %}
4.3 模板继承和包含

模板继承允许您定义一个基础模板,其他模板可以继承这个模板并扩展它。

示例代码:基础模板

html 复制代码
<!-- templates/base.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}My Website{% endblock %}</title>
</head>
<body>
    <header>
        {% block header %}Default Header{% endblock %}
    </header>
    <main>
        {% block content %}{% endblock %}
    </main>
    <footer>
        {% block footer %}Default Footer{% endblock %}
    </footer>
</body>
</html>

示例代码:继承基础模板

html 复制代码
<!-- templates/about.html -->
{% extends 'base.html' %}
{% block header %}About Us{% endblock %}
{% block content %}
    <p>This is the about page.</p>
{% endblock %}

模板包含允许您在模板中包含其他模板片段。

示例代码:模板包含

html 复制代码
<!-- templates/sidebar.html -->
<div class="sidebar">
    <h3>Sidebar</h3>
    <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/about">About</a></li>
    </ul>
</div>

<!-- templates/base.html -->
...
<body>
    {% include 'sidebar.html' %}
    <main>
        {% block content %}{% endblock %}
    </main>
...
4.4 模板变量和过滤器

模板变量用于在模板中传递数据,而过滤器则用于修改变量的显示方式。

示例代码:使用模板变量

html 复制代码
<!-- templates/user_profile.html -->
<p>Username: {{ user.username }}</p>
<p>Member since: {{ user.member_since|date('Y-m-d') }}</p>

示例代码:自定义过滤器

python 复制代码
from flask import Flask

app = Flask(__name)

@app.template_filter()
def capitalize_sentence(s):
    return s[0].upper() + s[1:].lower()

# 在模板中使用自定义过滤器
<p>{{ user.bio|capitalize_sentence }}</p>
4.5 总结

本章介绍了模板渲染的概念和Jinja2模板引擎的使用,包括模板继承、包含以及变量和过滤器的使用。

相关推荐
2601_962293241 小时前
Python自动化统计团队工作量并生成可视化仪表盘的脚本方案【指导】
python·数据分析·自动化·可视化·仪表盘
摇滚侠1 小时前
《SpringBoot 3:入门与应用实战》第 12 章 JDBC 与事务 使用 JdbcTemplate 阅读笔记 32
spring boot·笔记·后端
2601_962293792 小时前
OCRmyPDF批处理脚本:使用Python自动化复杂OCR任务
python·自动化·批处理脚本·ocrmypdf·ocr任务
步行cgn3 小时前
@Configuration 详解:Spring 配置类的核心注解
java·后端·spring
Java后端的Ai之路4 小时前
20、Python - 备忘录模式
开发语言·人工智能·python·外观模式·备忘录模式
2601_962077714 小时前
基于Python与NLP的新闻事件信息抽取实战:从NER到时空标准化
python·nlp·transformers·spacy·新闻事件抽取
桦说编程4 小时前
【AtomicAgent系列1】变异测试——过去做不起,现在 agent 做得起
后端·ai编程·vibecoding
JavaPub-rodert5 小时前
LangChain 从入门到 Agent 实战:用 Python 搭建一个真正能调用工具和知识库的 AI 助手
人工智能·python·langchain
郝学胜-神的一滴5 小时前
Qt 高级编程 045:坐标体系深度实战
开发语言·c++·windows·python·qt·程序人生
考虑考虑5 小时前
nginx打印请求日志
运维·后端·nginx