考虑到下面的路由,我发现如果参数 test 具有默认值,则通过 url_for 传递值时它不会改变>和重定向。我想要的是在渲染模板时更改参数 test ,具体取决于是否刚刚提交了表单。
@app.route('/view_story/<story_id>', methods=['GET', 'POST'])
def view_story(story_id, test='no'):
...
if current_user.is_authenticated:
if request.method == 'POST':
#Add stuff to database from the form.
return redirect(url_for('view_story', story_id=story_id, test='yes'))
return render_template('story.html', test=test)
答案 0 :(得分:1)
尝试以下代码:
@app.route('/view_story/<story_id>', defaults={"test": "no"})
@app.route('/view_story/<story_id>/<test>', methods=['GET', 'POST'])
def view_story(story_id, test):
...
if current_user.is_authenticated:
if request.method == 'POST':
#Add stuff to database from the form.
return redirect(url_for('view_story', story_id=story_id, test='yes'))
return render_template('story.html', test=test)