文章目录
基本使用
入口
python
# conding:utf-8
import os
from flask import Flask
app = Flask(__name__,template_folder='template')
app.config['SECRET_KEY'] = os.urandom(20)
from controlle.index02 import index02
app.register_blueprint(index02)
# 过滤器
@app.template_filter('add')
def add(input):
return input+1
# 全局函数
def myadd(a, b):
return a + b
app.jinja_env.globals.update(myadd=myadd)
if __name__ == '__main__':
app.run()
controlle.index02
python
from flask import Blueprint, render_template, session
index02 = Blueprint("index02", __name__)
@index02.route("/index02")
def index2_info():
session["username"] = "大周老师"
article = {
"title": "论Python语言的学习难度",
"count": 2001,
"content": "<strong>你好</strong>"
}
return render_template('index02.html', article=article)
渲染模版
python
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button>你好这里是我的第二个模块</button>
<h1>基本使用</h1>
<div>标题:{{article.title}}</div>
<div>阅读次数:{{article.count}}</div>
{% if article.count % 2 == 0 %}
<div>这是个偶数</div>
{% else %}
<div>这是个基数</div>
{% endif %}
{% set result1 = article.count / 100 %}
{% set result2 = result1 | int %}
<div>当前除以100的结果是: {{result2}}</div>
{% for i in range(result2) %}
<div>循环的每一项事: {{i+1}}</div>
{% endfor %}
<!--基本过滤器-->
<div>{{ article.content | safe }}</div>
<div>{{ 'hello word' | upper }}</div>
<div>{{ 'hello word' | title }}</div>
<div>{{ 'hello word' | lower }}</div>
<!-- 自定义过滤器 -->
<div>自定义过滤器{{ 1 | add }}</div>
<!-- 全局函数 -->
<div>全局函数{{ myadd(1,2) }}</div>
</body>
</html>