Flask--路由说明

from flask import Flask

app=Flask(__name__)

#同一个视图函数可以添加多个路由
@app.route('/')
@app.route('/index/')
def index():
	return 'hello world'

#设置访问方式
#注意:如果既可以使用GET,又可以使用POST请求,需要都声明出来 methods=['GET','POST']

@app.route('/index2/',methods=['GET','POST'])
def index2():
	return 'index2...'
'''
Map([<Rule '/index2/' (HEAD, GET, POST, OPTIONS) -> index2>,
 <Rule '/index/' (GET, HEAD, OPTIONS) -> index>,
 <Rule '/static/<filename>' (GET, HEAD, OPTIONS) -> static>])
'''
from flask import redirect,url_for
#重定向
@app.route('/login/')
def login():
	#重定向到首页
	#return redirect('/')
	#使用url_for函数,传入视图函数名称
	return redirect(url_for('index')) #视图名称

if __name__ == '__main__':
	#可以通过url_map来查看整个flask中路由信息
	print(app.url_map)
	'''
    Map([<Rule '/index/' (OPTIONS, HEAD, GET) -> index>,
        <Rule '/static/<filename>' (OPTIONS, HEAD, GET) -> static>])
    1./index/  ,  /static/........:表示访问的路径
    2.(OPTIONS,GET,HEAD):表示访问的方式  
    3.index,static:表示访问资源(视图函数,静态资源)  
        
    '''

	#开启debug模式
	app.run(debug=True)

猜你喜欢

转载自blog.csdn.net/weixin_44111377/article/details/92003168