Python全系列 教程
3567个小节阅读:5929.7k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
add_url_rule(rule,endpoint=None,view_func=None)
这个方法用来添加url与视图函数的映射。
如果没有填写endpoint
,那么默认会使用view_func
的名字作为endpoint
。
以后在使用url_for
的时候,就要看在映射的时候有没有传递endpoint
参数,如果传递了,那么就应该使用endpoint
指定的字符串,
如果没有传递,那么就应该使用view_func
的名字。
xdef my_list():
return "我是列表页"
app.add_url_rule('/list/',endpoint='sxt',view_func=my_list)
这个装饰器底层,其实也是使用add_url_rule
来实现url与视图函数映射的。
xxxxxxxxxx
from flask import Flask,url_for
app = Flask(__name__)
@app.route('/',endpoint='index')
def index():
print(url_for('show'))
print(url_for('index'))
return "Hello"
def show_me():
return "这个介绍信息!!"
# endpoint 没有设置,url_for中就写函数的名字,如果设置了,就写endpoint的值
app.add_url_rule('/show_me',view_func=show_me,endpoint='show')
# @app.route 底层就是使用的 add_url_rule
if __name__ =='__main__':
app.run(debug=True)
实时学习反馈
1. 在Flask中关于使用add_url_rule函数时必须填写的参数正确的是?
A path、endpoint、view_func
B path、endpoint
C path、view_func
D endpoint、view_func
2. 在Flask中关于使用add_url_rule函数与@app.route函数说法不正确的是?
A add_url_rule可以单独使用
B app.route可以单独使用
C add_url_rule与app.route都是映射函数
D app.route底层实现是add_url_rule
答案
1=>C 2=>B