设置
GAME_ID的post请求导航到多个页面之一,例如game页面(localhost:5000/game)或players页面(localhost:5000/players)。GAME_ID用于获取其他详细信息并呈现页面.目标
/game发出post请求时,将他们重定向到/login页面,但在会话中保留端点(game)和GAME_ID,以便在成功登录时,我可以将他们发送回他们正在访问的页面。我的方法是进行307重定向。在application.py中
@login_manager.unauthorized_handler
def intercept_unauthorized():
<Set NEXT_PAGE_FOR_REDIRECT and GAME_ID_FOR_REDIRECT into the session>
return redirect(url_for('login'))
@application.route("/login", methods=['GET', 'POST'])
def login():
#User sees the login page
if request.method=='GET':
<show the login page>
#User has submitted username and password on the login page
elif request.method=='POST':
<authentication code here>
if validUser:
#Get the page the user was trying to access e.g. 'game'
nextPage = session.get('NEXT_PAGE_FOR_REDIRECT')
session.pop('NEXT_PAGE_FOR_REDIRECT')
gameID = session.get('GAME_ID_FOR_REDIRECT')
session.pop('GAME_ID_FOR_REDIRECT')
return redirect(url_for(next_page, gameID=gameID), code=307)
@application.route("/game", methods=['POST'])
@login_required
def game():
if 'gameID' in request.form:
render_template('game.html', gameID=request.form['gameID'])问题
request.method所在的/game。但是,request.form中的数据是从login页面提交的数据,即用户名和密码。url_for行中设置的gameID,但它是以request.args发送的(并且在浏览器的地址栏中可见),而不是通过问题
/game的表单,以便将gameID添加到/game?发布于 2020-08-11 16:26:38
来自:https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307
HTTP 307临时重定向状态响应代码表示所请求的资源已临时移动到位置标头提供的URL。
重新使用原始请求的方法和主体来执行重定向请求。在您希望将方法更改为GET的情况下,请使用303参阅Other。当您想要给出一个PUT方法的答案时,这是非常有用的,这个方法不是上传的资源,而是一个确认消息(比如“您成功地上传了XYZ")。
307和302之间的唯一区别是307保证在重定向请求时方法和主体不会被更改。
因此,保留原来的request.form。您只需使用session来显示隐藏的ID,例如:
@application.route("/game", methods=['POST'])
@login_required
def game():
gameID = session.get('GAME_ID_FOR_REDIRECT')
session.pop('GAME_ID_FOR_REDIRECT')
render_template('game.html', gameID=gameID )当然,您必须将pop()从login()中删除
https://stackoverflow.com/questions/63360632
复制相似问题