为了账号安全,请及时绑定邮箱和手机立即绑定

flask 中url中重定向问题,参数传递

flask 中url中重定向问题,参数传递

胖猫CAT 2019-11-27 11:14:25
from flask import Flask, url_for from werkzeug.utils import redirect app = Flask(__name__) @app.route('/admin') def hello_admin():    return 'Hello Admin' @app.route('/guest/') def hello_guest(guest):    return 'Hello %s as Guest' % guest @app.route('/user/') def hello_user(name):    if name =='admin':       return redirect(url_for('hello_admin'))    else:       return redirect(url_for('hello_guest'), guest = name)       # if __name__ == '__main__': #    app.run(debug = True) if __name__ == '__main__':    app.run(debug=True)1、运行 http://127.0.0.1:5000/admin 显示 Hello Admin 运行正常2、但是运行http://127.0.0.1:5000/user/    显示如下错误:TypeErrorTypeError: hello_user() missing 1 required positional argument: 'name' 3、再次运行http://127.0.0.1:5000/user/admin   显示如下错误: Not FoundThe requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. 4、再次运行http://127.0.0.1:5000/user/aaa显示如下错误:Not FoundThe requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. 第3、4次运行中是不是我的输入是错误导致的?那么参数应该怎么传呢? 5、因为以上代码是教程中到源代码,应该是没有错的。后来我修改了源代码中@app.route('/user/<name>')加入了<name>这个参数。运行  http://127.0.0.1:5000/admin和 http://127.0.0.1:5000/user/admin 都显示Hello Admin 运行正常。但是运行http://127.0.0.1:5000/user/kjkj显示如下错误:TypeErrorTypeError: redirect() got an unexpected keyword argument 'guest'请问问题到底出在哪里呢??
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 3279 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信