Flask重定向,使用带有参数的url_for

问题描述 投票:0回答:2

我正在尝试将函数重定向到她自己,但是更改了参数的值。

这是我的代码:

@app.route('/', methods=["GET", "POST"])
def index(save=False):
    form = FormFields()
    if form.validate_on_submit:
        csv = Csv(form)
        csv.savecsv()
        return redirect(url_for('index', save=True))
    else:
        print(form.errors)
    return render_template('form.html', form=form, save=save)

我希望重定向时,save变量为True,但始终为False。

在表单代码中,我有这个:

{% if save %}
    <script type="text/javascript"> alert("Saved!");</script>
{% endif %}
python flask flask-wtforms wtforms
2个回答
0
投票
我找到了替代解决方案

我更改了:

return redirect(url_for('index', save=True))

为此]

return render_template('form.html', form=form, save=True)

它将制作新的渲染图

并且我在form.html代码中添加了它

$(document).ready(function() { $('input').val(""); });

这解决了我的问题,原因是我收到确认消息(当保存为True时,并且清除了字段]

但是如果有人真的需要return redirect url_for可以毫无问题地使用Suman Niroula的解决方案


0
投票
在路由中使用默认值:
© www.soinside.com 2019 - 2024. All rights reserved.