Python中Flask模板的使用与高级技巧详解
作者:码农阿豪@新空间
在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看Flask模板的具体应用吧
一、模板渲染基础
1.1 为什么需要模板引擎
在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题:
- 代码难以维护
- 前后端耦合严重
- 无法复用HTML组件
- 缺乏逻辑控制能力
Flask内置了Jinja2模板引擎,完美解决了这些问题。
1.2 第一个模板渲染示例
首先创建项目结构:
myapp/
├── app.py
└── templates/
└── index.html
app.py内容:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
user = {'username': '张三', 'age': 25}
posts = [
{'title': '第一篇', 'content': '内容1'},
{'title': '第二篇', 'content': '内容2'}
]
return render_template('index.html', user=user, posts=posts)
if __name__ == '__main__':
app.run(debug=True)
templates/index.html内容:
<!DOCTYPE html>
<html>
<head>
<title>{{ user.username }}的主页</title>
</head>
<body>
<h1>欢迎, {{ user.username }}!</h1>
<p>年龄: {{ user.age }}</p>
<h2>文章列表</h2>
<ul>
{% for post in posts %}
<li>{{ post.title }} - {{ post.content }}</li>
{% endfor %}
</ul>
</body>
</html>1.3 模板渲染原理
render_template()函数的工作流程:
- 在templates目录查找指定模板文件
- 解析模板中的变量和逻辑
- 将上下文变量传入模板
- 生成最终HTML响应
二、模板访问对象属性
2.1 访问字典属性
<p>用户名: {{ user['username'] }}</p>
<p>年龄: {{ user.get('age', 18) }}</p> <!-- 带默认值 -->
2.2 访问对象属性
假设我们有一个User类:
class User:
def __init__(self, username, email):
self.username = username
self.email = email
模板中可以这样访问:
<p>用户名: {{ user.username }}</p>
<p>邮箱: {{ user.email }}</p>
2.3 访问列表和元组
<p>第一篇文章: {{ posts[0].title }}</p>
<p>最后一篇文章: {{ posts[-1].title }}</p>
2.4 特殊变量访问
<p>当前时间: {{ config.DEBUG }}</p> <!-- 访问Flask配置 -->
<p>请求方法: {{ request.method }}</p> <!-- 访问请求对象 -->
<p>会话信息: {{ session.get('user_id') }}</p>
<p>闪现消息: {{ get_flashed_messages() }}</p>
三、过滤器的使用
3.1 内置过滤器大全
Jinja2提供了丰富的内置过滤器:
<!-- 字符串处理 -->
<p>{{ "hello"|capitalize }}</p> <!-- Hello -->
<p>{{ "HELLO"|lower }}</p> <!-- hello -->
<p>{{ "hello world"|title }}</p> <!-- Hello World -->
<p>{{ "hello"|replace("e", "a") }}</p> <!-- hallo -->
<!-- 列表处理 -->
<p>{{ [1,2,3]|length }}</p> <!-- 3 -->
<p>{{ [1,2,3]|first }}</p> <!-- 1 -->
<p>{{ [1,2,3]|last }}</p> <!-- 3 -->
<p>{{ [1,2,3]|join("|") }}</p> <!-- 1|2|3 -->
<!-- 数值处理 -->
<p>{{ 3.1415926|round(2) }}</p> <!-- 3.14 -->
<p>{{ 1000|filesizeformat }}</p> <!-- 1000 Bytes -->
<p>{{ 0.85|float }}</p> <!-- 0.85 -->
<!-- 日期处理 -->
<p>{{ user.create_time|datetimeformat }}</p>
<p>{{ user.create_time|datetimeformat('%Y-%m-%d') }}</p>
<!-- HTML处理 -->
<p>{{ "<script>alert(1)</script>"|escape }}</p>
<p>{{ "Markdown text"|markdown }}</p>
<p>{{ "https://example.com"|urlencode }}</p>
3.2 自定义过滤器
在app.py中注册自定义过滤器:
@app.template_filter('reverse')
def reverse_filter(s):
return s[::-1]
@app.template_filter('format_phone')
def format_phone(phone):
return f"{phone[:3]}-{phone[3:7]}-{phone[7:]}"
模板中使用:
<p>{{ "hello"|reverse }}</p> <!-- olleh -->
<p>{{ "13812345678"|format_phone }}</p> <!-- 138-1234-5678 -->Flask模板高级技巧
1.控制语句
条件判断
{% if user.age < 18 %}
<p>未成年用户</p>
{% elif user.age > 60 %}
<p>老年用户</p>
{% else %}
<p>成年用户</p>
{% endif %}
循环语句
<table>
<thead>
<tr>
<th>序号</th>
<th>标题</th>
<th>内容</th>
</tr>
</thead>
<tbody>
{% for post in posts %}
<tr class="{{ loop.cycle('odd', 'even') }}">
<td>{{ loop.index }}</td>
<td>{{ post.title }}</td>
<td>{{ post.content }}</td>
</tr>
{% else %}
<tr>
<td colspan="3">暂无文章</td>
</tr>
{% endfor %}
</tbody>
</table>循环变量说明:
- loop.index: 当前迭代次数(从1开始)
- loop.index0: 当前迭代次数(从0开始)
- loop.revindex: 反向迭代次数
- loop.first: 是否第一次迭代
- loop.last: 是否最后一次迭代
- loop.length: 序列长度
宏定义(模板函数)
定义宏:
{% macro render_comment(comment) %}
<div class="comment">
<p>{{ comment.author }} 说:</p>
<blockquote>{{ comment.content }}</blockquote>
</div>
{% endmacro %}
使用宏:
{{ render_comment(comment) }}
<!-- 导入其他模板中的宏 -->
{% from 'macros.html' import render_comment %}
2.模板继承
基础模板(base.html)
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}默认标题{% endblock %}</title>
{% block head %}
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" rel="external nofollow" >
{% endblock %}
</head>
<body>
<div class="container">
{% block content %}
<h1>默认内容</h1>
{% endblock %}
</div>
{% block footer %}
<footer>
<p>© 2023 My App</p>
</footer>
{% endblock %}
</body>
</html>子模板继承
{% extends "base.html" %}
{% block title %}用户主页 - {{ super() }}{% endblock %}
{% block head %}
{{ super() }}
<style>
.profile { color: blue; }
</style>
{% endblock %}
{% block content %}
<div class="profile">
<h1>{{ user.username }}的个人资料</h1>
<p>年龄: {{ user.age }}</p>
</div>
{% endblock %}
{% block footer %}
<footer>
<p>© 2023 用户中心</p>
</footer>
{% endblock %}
包含其他模板
<!-- 包含头部 -->
{% include 'header.html' %}
<!-- 带参数包含 -->
{% include 'user_card.html' with user=current_user %}
<!-- 忽略缺失模板 -->
{% include 'sidebar.html' ignore missing %}
3.加载静态文件
静态文件组织
标准项目结构:
myapp/
├── app.py
├── static/
│ ├── css/
│ ├── js/
│ └── images/
└── templates/
引用静态文件
<!-- CSS文件 -->
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" rel="external nofollow" >
<!-- JavaScript文件 -->
<script src="{{ url_for('static', filename='js/main.js') }}"></script>
<!-- 图片 -->
<img src="{{ url_for('static', filename='images/logo.png') }}" alt="Logo">
<!-- 使用缓存清除 -->
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css', v=1.0) }}" rel="external nofollow" >
静态文件版本控制
在配置中添加版本号:
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600 # 1小时缓存 app.config['STATIC_VERSION'] = '1.0.0'
模板中使用:
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}?v={{ config.STATIC_VERSION }}" rel="external nofollow" >
使用CDN资源
{% if config.CDN_ENABLED %}
<script src="https://cdn.example.com/jquery/3.6.0.min.js"></script>
{% else %}
<script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
{% endif %}以上就是Python中Flask模板的使用与高级技巧详解的详细内容,更多关于Python Flask模板的资料请关注脚本之家其它相关文章!
