table of Contents
URL mapping and view functions
Passing parameters
Syntax transmission parameters are: /<参数名>/
routing defines the parameters, then the view function, the same name should be defined in the parameters received, such as:
# @app.route('/article/<article_id>/')
@app.route('/article/<int:article_id>/') # 限制数据类型
def article_detail(article_id):
return '第 {} 篇文章的详情'.format(article_id)
Transmitting specified data type
Syntax: <类型:参数名>
which types are the following:
- String : the default data type, accepting no slash '/' Text
- int : Integer
- float : floating point type
- path : the path to accept, and string the like, but can accept a slash '/'
- uuid : only accepts a string in the uuid, uuid is a string that are unique in the world, generally the service sector as the primary key of the table
- the any : a can
url
specify multiple paths, as in the following example:
# /blog/<id>/
# /user/<id>/
@app.route('/<any(blog, user):url_path>/<int:id>/')
def detail(url_path, id):
if url_path == 'blog':
return '博客详情: {{ {}: {} }}'.format(url_path, id)
else:
return '用户详情: {{ {}: {} }}'.format(url_path, id)
Receiving user parameters passed
- Using the path of (the embedding parameter to the path), is above spoken
- Use in the form of a query string (
?key=value
) to pass, and a plurality of key-value, with the&
spaced apart, such as:?name=long&age=18
from flask import request
@app.route('/select_str/')
def select_str():
wd = request.args.get('wd')
return '你通过查询字符串的方式传递的参数是: %s' % wd
# http://127.0.0.1:5000/select_str/?wd=python