flask框架jinja2模板与模板继承实例分析
作者:我是猪行不 时间:2023-08-26 22:21:34
本文实例讲述了flask框架jinja2模板与模板继承。分享给大家供大家参考,具体如下:
jinja2模板
from werkzeug.contrib.cache import SimpleCache
from flask import Flask, request, render_template,redirect,abort, url_for
CACHE_TIME = 300
cache = SimpleCache()
cache.timeout = CACHE_TIME
app = Flask(__name__)
@app.before_request
def return_cached():
if not request.values:
response = cache.get(request.path)
if response:
print("Got the page from cache!")
return response
print("Will load the page!")
@app.after_request
def cache_response(response):
print("aaaaaaaaaaaaaaaaaaaaaa")
if not request.values:
cache.set(request.path, response, CACHE_TIME)
return response
@app.teardown_request
def teardown_request(response):
print('llllllllllllllllllllllll')
return "llllllllllllllllllllll"
# @app.route('/')
@app.route('/get_index')
def index():
return render_template('jinja2.html', a_variable="Developer", navigation=["http://www.163.com", "www.baidu.com"])
if __name__ == '__main__':
app.run(port=8000)
jinja2.html必须在templates文件夹下,例子如下:
<!DOCTYPE html>
<html>
<head>
<title>jinja2_test</title>
</head>
<body>
<ul id="navigation">
{% for item in navigation %} #表达式
<li href='{{ item }}'>{{ item }}</li> #输出变量
{% endfor %}
</ul>
<h1>HelloWorld</h1>
{{a_variable}}#输出变量
{# aaaa #}#模板注释,加载自动删除
</body>
</html>
jinja2模板继承
父亲:
<!DOCTYPE html>
<html>
<head>
<title>模板继承</title>
</head>
<body>
<span>这是基模板</span>
<div id="content">{% block content %}{% endblock %}</div>
</body>
</html>
用{% block content %}{% endblock %}
包含jinja2的字模板块;
子:
<!DOCTYPE html>
<html>
<head>
<title>模板继承</title>
</head>
<body>
{% extend "jinja2_模板继承.html"%}
{% block content %}
<p class="importtant">我在子模板</p>
</body>
</html>
{% extends "jinja2_模板继承.html"%}
标签是这里的关键,告诉模板引擎这个模板继承自另外一个模板。该标签必须是子模板的第一个标签,解释器会自动将父亲的内容复制到子模板中!
结果应该是这样:
<!DOCTYPE html>
<html>
<head>
<title>模板继承</title>
</head>
<body>
<span>这是基模板</span>
<div id="content">
<p class="importtant">我在子模板</p>
</div>
</body>
</html>
希望本文所述对大家基于flask框架的Python程序设计有所帮助。
来源:https://blog.csdn.net/weixin_42694291/article/details/82892691
标签:flask框架,jinja2模板,模板继承
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Python面向对象编程oop思想心得
2021-10-25 16:45:29
SQL update 多表关联更新的实现代码
2024-01-22 01:44:06
Python中socket网络通信是干嘛的
2023-12-16 02:57:03
Python入门教程3. 列表基本操作【定义、运算、常用函数】 <font color=red>原创</font>
2023-07-15 13:09:19
简单了解python装饰器原理及使用方法
2023-11-02 11:55:02
解决pytorch 模型复制的一些问题
2022-04-23 03:57:58
Python注释、分支结构、循环结构、伪“选择结构”用法实例分析
2021-01-15 14:45:25
![](https://img.aspxhome.com/file/2023/7/69587_0s.png)
Python装饰器实现方法及应用场景详解
2022-04-30 22:57:49
JS扩展方法实例分析
2024-04-25 10:35:54
python机器学习库常用汇总
2022-05-17 11:55:20
如何修改Linux的下MySQL 5.0的默认连接数
2012-01-29 18:07:04
Mysql数据表中的蠕虫复制使用方法
2024-01-24 15:06:06
![](https://img.aspxhome.com/file/2023/9/114949_0s.png)
Python之OptionParser模块使用详解
2021-03-08 14:07:48
Python干货实战之八音符酱小游戏全过程详解
2021-08-20 11:21:27
![](https://img.aspxhome.com/file/2023/6/117846_0s.png)
win10环境下使用Hyper-V进行虚拟机创建的教程(图解)
2022-08-01 02:25:06
![](https://img.aspxhome.com/file/2023/5/131875_0s.jpg)
JavaScript 函数惰性载入的实现及其优点介绍
2024-04-16 09:25:37
关于python的对象序列化介绍
2023-07-27 05:02:31
pytest实现测试用例参数化
2023-12-10 19:01:21
对Python新手编程过程中如何规避一些常见问题的建议
2021-04-01 19:27:16
python中pip安装、升级以及升级固定的包
2021-07-08 02:29:11
![](https://img.aspxhome.com/file/2023/9/65559_0s.png)