Mapping 3_Flask the URL and view function

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:

  1. String : the default data type, accepting no slash '/' Text
  2. int : Integer
  3. float : floating point type
  4. path : the path to accept, and string the like, but can accept a slash '/'
  5. 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
  6. the any : a can urlspecify 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

  1. Using the path of (the embedding parameter to the path), is above spoken
  2. 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

Guess you like

Origin www.cnblogs.com/nichengshishaonian/p/11610011.html