使用 flask 和 wtforms 在单个页面中的多个提交按钮

Multiple submit button in a single page using flask and wtforms

我正在编写一个 costom 404 处理页面,需要三个按钮(“返回上一页”;“转到主页”;“访问我们的帮助中心”),但是我 运行 进入了一些麻烦,这是我想出来的和我要解决的问题

我的404.HTML

{% extends "layout.html"%}
{% block content %}
  <h1>OOF</h1>
  <p>We are sorry, but you have reached this page in error.</p>
  <p>Please try the action again and if the problem continues, contact Customer Support.</p>
  <p>404 Error - Document not found</p>
  
    <form method="POST" action="">
      {{ form.hidden_tag() }}
      <fieldset class="form-group">
        <legend class="border-bottom mb-4"></legend>
        
        
      </fieldset>
        {{ form.submit(class="btn btn-outline-info") }}
        {{ form.submit(class="btn btn-outline-info") }}
        {{ form.submit(class="btn btn-outline-info") }}
    </form>
    
{% endblock content %}

我的forms.py

class PreviousPage(FlaskForm):
  submit = SubmitField("Go back to the previous page")
  submit = SubmitField("Go to Home")
  submit = SubmitField("Visit our Help Center")

我的routes.py(主要功能)

@app.errorhandler(404)
def page_not_found(e):
  form=PreviousPage()
  if form.validate_on_submit():
    return redirect(url_for('home'))
  return render_template('404.html',form=form)

这是我得到的 A picture of my 404 page

按钮的布局完全符合我的预期,但按钮上的文本不正确,因为它全部打印出最后一个按钮的文本。 我面临的另一个问题是不知道如何修改我的 routes.py 文件,以便这三个按钮可以将用户重定向到三个相同的页面。

谢谢

form.py

class PreviousPage(FlaskForm):
  submit_pre = SubmitField("Go back to the previous page")
  submit_home = SubmitField("Go to Home")
  submit_help = SubmitField("Visit our Help Center")

404.HTML

{% extends "layout.html"%}
{% block content %}
  <h1>OOF</h1>
  <p>We are sorry, but you have reached this page in error.</p>
  <p>Please try the action again and if the problem continues, contact Customer Support.</p>
  <p>404 Error - Document not found</p>
  
    <form method="POST" action="">
      {{ form.hidden_tag() }}
      <fieldset class="form-group">
        <legend class="border-bottom mb-4"></legend>
        
        
      </fieldset>
        {{ form.submit_pre(class="btn btn-outline-info") }}
        {{ form.submit_home(class="btn btn-outline-info") }}
        {{ form.submit_help(class="btn btn-outline-info") }}
    </form>
    
{% endblock content %}

至此,第一个问题解决了

@app.errorhandler(404)
def page_not_found(e):
    form = PreviousPage()
    if form.validate_on_submit():
        if form.submit_pre.data:
            return redirect(url_for('pre_page'))
        if form.submit_home.data:
            return redirect(url_for('home_page'))
        if form.submit_help.data:
            return redirect(url_for('help_page'))
    return render_template('404.html', form=form)

至此,第二个问题就解决了