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

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.

 34次运行中是不是我的输入是错误导致的?那么参数应该怎么传呢?

 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'

请问问题到底出在哪里呢??

胖猫CAT
浏览 3284回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP