Flask 重新路由总是使用默认函数参数
Flask reroute always using default function argument
给定一条如下所示的路由,我发现如果参数 test 有一个默认值,当通过 [=] 传递值时它不会改变13=]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)
试试下面的代码:
@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)
给定一条如下所示的路由,我发现如果参数 test 有一个默认值,当通过 [=] 传递值时它不会改变13=]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)
试试下面的代码:
@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)