Detailed routing of flask

Introduction to routing

The use of routing in the flask program, which we call registration route, is to register the route using the app.route () decorator provided by the program instance.

@app.route('/student_list/')
def student_list():
    return 'students'

The essence and parameters of routing

from flask import Flask, url_for, redirect
 
app = Flask(__name__)
 
 
def index(nid):
    print(nid, type(nid))
    return 'ojbk'
 
 
# 指定类型为int或者string类型
app.add_url_rule('/index/<int:nid>', view_func=index, endpoint='index1', methods=['post', 'get'])
# app.route的本质就在执行add_url_rule这个中的rule是路由,endpoint是路由别名,view_func是响应函数
# app.add_url_rule('/index/<int:nid>', view_func=index, methods=['post', 'get'])
 
 
@app.route('/login', methods=['get', 'post'])
def login():
    # 用endpoint取路由要用url_for 在flask中导入,也就是反向解析
    print(url_for("index1"))
    return redirect(url_for("index1"))  # url_for通过路由的别名反向解析出来路由的url
 
 
# 路由参数;methods,可以控制该方法能有哪些请求方式可以访问
app.add_url_rule("/index", endpoint="index1", view_func=index, methods=["POST", "GET"])
# 路由参数:有名分组,app.add_url_rule("/index/<int:nid>"响应函数必须用nid来接收
 
if __name__ == '__main__':
    app.run()
 
'''
总结:
1 @app.route("/login") 的本质是app.add_url_rule("/login",view_func=login),所以我们就可以用这两个方式来添加路由
2 路由的参数,
    2.1 endpoint,做是反向解析,如果上面添加路由的时候,没有传递endpoint就是使用响应函数的函数名,反向解析用url_for(),做解析,这个url_for必须在flask里面导入
    2.2 methods=["POST","GET"],该参数控制路由允许哪些请求方法访问,如果不传,默认只能GET方法
    2.3 路由以及路由路由转化器。"/index/<int:nid>",<参数的类型:用哪个变量来接收>,响应函数中的形参的名字必须转化器中一致。
 
'''

Dynamic routing

http://127.0.0.1:5000/student_list/2/

There is a variable part in the path to achieve the purpose of passing parameters, we call it dynamic routing

@app.route('/student_list/<student_id>/')
def student_list(student_id):
    return '学生{}号的信息'.format(student_id)

Dynamic routing filtering

You can limit the data type of the parameter, for example, the student_id must be an integer type

http://127.0.0.1:5000/student_list/2/
@app.route('/student_list/<int:student_id>/')
def article_detail(student_id):
    print(student_id, type(student_id))
    return '学生{}号的信息'.format(student_id)

There are mainly several types of filtering:

  string: The default data type, to receive a string without any slash "/"

  int: Integer

  float: Floating point

  path: Similar to the string type, but accepts slashes, such as: can accept parameters / aa / bb / cc / multiple together

  uuid: Only accept strings in uuid format,

✔ Tip: uuid is the only string in the universe

The above constraints are in the following format , int in the example can be replaced by string,float,path,uuid:

@app.route('/student_list/<int:student_id>/')
def article_detail(student_id):
    return '学生{}号的信息'.format(student_id)

any: Multiple paths can be specified, as in the following example

The variable name of url_path is defined by yourself

@app.route('/<any(student,class):url_path>/<id>/')
def item(url_path, id):
    if url_path == 'student':
        return '学生{}详情'.format(id)
    else:
        return '班级{}详情'.format(id)

What are the applicable scenarios of dynamic routing?

If you want to increase the exposure of the website, you can consider using dynamic routing, because the path is used as a parameter, the search engine algorithm will define you as a static page, will not change frequently, which is beneficial to search engine optimization. But if it is the company's internal management system, there is no need to use dynamic routing, because the internal system does not require exposure.

Keywords :

  • Above we accept the parameter using the path (path) form, this form of parameter transmission is called dynamic routing parameter transmission, which is conducive to the optimization of search engines.

Use of url_for ()

Use the feature that the name of the view function generally does not change, according to the name of the view function to dynamically and accurately obtain the URL, in order to facilitate development and use.

url_for('视图函数名字')   # 输出该视图函数url

Specific examples:

from flask import Flask,url_for
 
app = Flask(__name__)
app.config.update(DEBUG=True)
 
@app.route('/')
def demo1():
    print(url_for("book"))  # 注意这个引用的是视图函数的名字 字符串格式
    print(type(url_for("book")))
 
    return url_for("book")
 
 
@app.route('/book_list/')
def book():
 
    return 'flask_book'
 
if __name__ ==  "__main__":
    app.run()

How does url_for handle dynamic view functions

If you want to obtain a dynamic route, you must assign a value to the dynamic path part in the form of keyword arguments. Note that the dynamic path part must be assigned a value.

Case:

@app.route('/demo2/')
def demo2():
 
    student_url = url_for('student', id=5, name='mark') # id 就是动态path的key 必须赋值,                                                         # name 将作为查询字符串传入
    print(student_url)
 
    return student_url
 
 
@app.route('/student/<int:id>/')
def student(id):
    return 'student {}'.format(id)

Console output:


Browser output:

url_for how to add query string for url

If you want to spell out the query string behind the path, put it in url_for () as a parameter in the form of keyword argument, it will automatically spell the path

Case:

@app.route('/demo3/')
def demo3():
    school_url = url_for('school', school_level='high', name='college')
    # 具体要拼接的查询参数 以关键字实参的形式写在url_for里
    print(school_url)
 
    return school_url
 
@app.route('/school/')
def school():
 
    return 'school message'

Console output:

Browser output:

Custom converter (non-focus)

# 非重点
#1 写类,继承BaseConverter
#2 注册:app.url_map.converters['regex'] = RegexConverter
# 3 使用:@app.route('/index/<regex("\d+"):nid>')  正则表达式会当作第二个参数传递到类中
 
from flask import Flask, url_for
from werkzeug.routing import BaseConverter
 
app = Flask(import_name=__name__)
 
class RegexConverter(BaseConverter):
    """
    自定义URL匹配正则表达式
    """
    def __init__(self, map, regex):
        super(RegexConverter, self).__init__(map)
        self.regex = regex
 
    def to_python(self, value):
        """
        路由匹配时,匹配成功后传递给视图函数中参数的值
        """
        print("to_python",value,type(value))
        return int(value)+1
 
    def to_url(self, value):
        """
        使用url_for反向生成URL时,传递的参数经过该方法处理,返回的值用于生成URL中的参数
        """
        val = super(RegexConverter, self).to_url(value)
        return val+"222"
 
# 添加到flask中
app.url_map.converters['regex'] = RegexConverter
# 正则匹配处理结果,要交给to_python,to_python函数可以对匹配处理结果做处理
@app.route('/index/<regex("\d+"):nid>')
def index(nid):
    print("index",nid,type(nid))
    print(url_for('index', nid='888'))
    return 'Index'
 
if __name__ == '__main__':
    app.run()

to sum up:

1 导入from werkzeug.routing import BaseConverter
2 我写个继承BaseConverter。实现3个方法,def __init__ , def to_python , def to_url
3 将上面的类注册到app.url_map.converters['regex'] = RegexConverter中
4 然后就可以在路由转化器中使用3中的regex("传正则")
5 当路由被访问以后。regex("传正则")会匹配结果,把结果传递to_python,我们可以进行再次处理,to_python处理好的结果,会传递给响应函数的形参
6 当用url做反向解析的时候,传递给路由转化器的参数,会经过to_url,进行处理。处理以后,在拼接到路由。

Guess you like

Origin www.cnblogs.com/cnhyk/p/12755927.html