美文网首页
3、flask-url构建

3、flask-url构建

作者: 许忠慧 | 来源:发表于2023-08-22 14:17 被阅读0次

基本语法:

from flask import Flask, redirect, url_for

# 创建一个app实例
app = Flask(__name__)


@app.route('/admin')
def hello_admin():
    return 'hello admin'


@app.route('/other/<name>')
def hello_other(name):
    return f'hello {name}'


@app.route('/users/<userName>')
def root(userName):
    if userName == 'admin':
        return redirect(url_for('hello_admin'))    # url_for 构建一个没有参数的url
    else:
        return redirect(url_for('hello_other', name=userName))  # url_for 构建一个有参数的url



if __name__ == '__main__':
    app.run(debug=True)

使用http://localhost:port/users/admin访问

使用http://localhost:port/users/zhangasn访问

ps:

redirect 是重定向方法,可以让你使用改路由去访问一个指令url的的连接, 这个方法还是挺好的。本章只当学习重定向了

相关文章

网友评论

      本文标题:3、flask-url构建

      本文链接:https://www.haomeiwen.com/subject/osbppdtx.html