1.配置
- 查询字符串的形式传参
@app.route('/book/list')
def book_list():
page = request.args.get('page',default=1,type=int)
return f"您获取的是{page}的图书列表!"
if __name__ == '__main__':
app.run()
3.HTML模版渲染
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
app.run()
4.使用HTML传参
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>中科汇成</title>
</head>
<body>
<h1>您的book_id是{{ blog_id }}</h1>
</body>
</html>
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/blog/<int:blog_id>')
def blog(blog_id):
return render_template('blog.html',blog_id=blog_id)
if __name__ == "__main__":
app.run()
5.类属性传递
from flask import Flask,render_template
app = Flask(__name__)
class User:
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return f"User(name={self.name},age={self.age})"
@app.route('/')
def hellow_world():
user = User(name='张三',age=18)
return render_template('index.html',user=user)
if __name__ == "__main__":
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>中科汇成</title>
</head>
<body>
用户姓名{{ user.name }}
用户年龄{{ user.age }}
</body>
</html>
6.字典类型数据传递
from flask import Flask,render_template
app = Flask(__name__)
class User:
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return f"User(name={self.name},age={self.age})"
@app.route('/')
def hellow_world():
user = User(name='张三',age=18)
person = {
"name": "李四",
"age": 20
}
return render_template('index.html',user=user,person = person)
if __name__ == "__main__":
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>中科汇成</title>
</head>
<body>
{{ person.name }}
{{ person.age }}
{{ person['name'] }}
{{ person['age'] }}
</body>
</html>
7.过滤器(传参之前先抛给一个函数处理)
from flask import Flask,render_template
app = Flask(__name__)
class User:
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return f"User(name={self.name},age={self.age})"
@app.route('/filter')
def filter():
user = User(name='张三11111',age=18)
return render_template('filter.html',user=user)
if __name__ == "__main__":
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ user.name|length }}
</body>
</html>
8.自定义过滤器
from flask import Flask,render_template
from datetime import datetime
app = Flask(__name__)
class User:
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return f"User(name={self.name},age={self.age})"
def datetime_format(value,format='%Y-%d-%m %H:%M'):
return value.strftime(format)
app.add_template_filter(datetime_format,'dformat')
@app.route('/filter')
def filter():
user = User(name='张三11111',age=18)
mytime = datetime.now()
return render_template('filter.html',user=user,mytime = mytime)
if __name__ == "__main__":
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ mytime|dformat }}
</body>
</html>
9.流程控制
from flask import Flask,render_template
from datetime import datetime
app = Flask(__name__)
@app.route('/control')
def control():
return render_template('control.html',age=17)
if __name__ == "__main__":
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if age < 18 %}
<p>未成年</p>
{% elif age==18 %}
<p>成年</p>
{% else %}
<p>老年</p>
{% endif %}
</body>
</html>
- 控制语句2
from flask import Flask,render_template
from datetime import datetime
app = Flask(__name__)
@app.route('/control')
def control():
books = [
{"name": "book1", "author": "author1"},
{"name": "book2", "author": "author2"},
]
return render_template('control.html',books=books)
if __name__ == "__main__":
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>控制台</h1>
{% for book in books %}
<div>名称:{{ book.name }},作者:{{ book.author }}</div>
{% endfor %}
</body>
</html>