pro*_*.io 1 python routing flask
我实际上是在用 Flask 创建一个应用程序,但我遇到了有关我的路由的问题。
我的情况很简单:用户输入一个令牌来验证自己。一旦他点击了authentication,一个有角度的 HTTP 请求就会使用 POST 将他的令牌发送到 Python 服务器。在那里,如果他被授予访问权限,则使用render_template;显示主页。否则登录保持静止。
但是,当用户对自己进行身份验证时,我在命令行上看到 POST 成功,身份验证成功,但页面只是停留在登录状态并且不会重定向到主页,就好像第二个render_template不起作用一样。请帮忙!
@app.route('/')
def index():
if not session.get('logged_in'):
return render_template('auth.html') # this is ok.
else:
return render_template('index.html') # this does not work
@app.route('/login', methods=['POST','GET'])
def login():
tok = request.form['token']
if (check_token(tok) == "pass"): # check_token is a function I've implemented
# to check if token is ok=pass, ko=fail
session['logged_in'] = True
else:
flash("wrong token")
return index()
Run Code Online (Sandbox Code Playgroud)
您的login处理程序不应index直接调用。它应该返回一个重定向到索引。
return redirect('/')
Run Code Online (Sandbox Code Playgroud)
或更好:
return redirect(url_for('index'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4275 次 |
| 最近记录: |