当我尝试在flask中使用redirect和url_for函数时,我得到一个重定向循环,如下所示:
@app.route('/edit/<id>' , methods=['POST', 'GET'])
def edit (id):
#Getting user by primary key:
post = Post.query.get(id)
if request.method == 'POST':
post.title = request.form['title']
post.text = request.form['text']
db.session.commit()
flash('New entry was successfully posted')
return redirect(url_for('edit', id=id, post=post))
更新:已解决http://techarena51.com/index.php/flask-sqlalchemy-tutorial/
答案 0 :(得分:0)
我认为如果用户提交了POST请求,您只想重定向?我认为你的代码应该是:
@app.route('/edit/<id>' , methods=['POST', 'GET'])
def edit (id):
#Getting user by primary key:
post = Post.query.get(id)
if request.method == 'POST':
post.title = request.form['title']
post.text = request.form['text']
db.session.commit()
flash('New entry was successfully posted')
return redirect(url_for('edit', id=id, post=post))
# The rest of this code is executed when a GET is performed
return render_template("edit.html",id=id, post=post)
否则,是的,每当访问时,您当前的代码将重定向到相同的路由。我已经更新了我的示例,以显示在请求方法不是POST时如何呈现编辑模板。