用烧瓶中的数据参数重定向

Ton*_*ony 3 python url-routing flask

我正在尝试学习flask,遇到了以下问题。这是我试图实现的示例。

@app.route('/poll', methods = ['GET', 'POST'])
def poll():
    form = PollForm()

    if form.validate_on_submit():
        return render_template('details.html', form = form)

    return render_template('poll.html', form=form)
Run Code Online (Sandbox Code Playgroud)

但我想为details.html使用不同的网址映射,为此,我创建了另一条路线,

@app.route('/details/<form>')
def details(): 
   return render_template('details.html', form = form):
Run Code Online (Sandbox Code Playgroud)

为了使用这个我已经使用了

return redirect(url_for('details', form=form))
Run Code Online (Sandbox Code Playgroud)

在if条件内的poll方法中。当我尝试从detail.html访问相同文件时,我无法将其作为对象获取。当尝试用字符串替换form时,它工作正常。您能否建议一些机制来将表单作为/ details路由内的对象进行访问?

编辑

我问这样的事情是可能的。

 @app.route('/poll', methods = ['GET', 'POST'])
    def poll():
        form = PollForm()

        if form.validate_on_submit():
        @app.route('/details')
            return render_template('details.html', form = form)

        return render_template('poll.html', form=form)
Run Code Online (Sandbox Code Playgroud)

每当我们进入if条件时,URL将是/ poll / details。或者是否有任何方法可以使这种URL嵌套,从根url开始,然后根据业务逻辑添加子url。

Mar*_*ers 7

您不能仅将表单对象放入URL中,不能。A redirect()是一个响应,告诉浏览器加载另一个URL,您不能轻易将form对象塞入URL路径元素中。

如果您不需要在浏览器位置栏中看到其他URL,请不要使用重定向,而只需调用其他函数即可:

def details(form): 
   return render_template('details.html', form = form):

@app.route('/poll', methods = ['GET', 'POST'])
def poll():
    form = PollForm()

    if form.validate_on_submit():
        return details(form)

    return render_template('poll.html', form=form)
Run Code Online (Sandbox Code Playgroud)

如果确实需要在浏览器中使用其他URL,则将<form>元素发布到/details路由,而不是/poll路由。