简体   繁体   English

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

[英]Multiple submit button in a single page using flask and wtforms

I am writing a costom 404 handling page and need three button("Go back to the previous page";"Go to Home";"Visit our Help Center"), however I ran into some trouble, here is what I came up with and the problem I want to solve我正在写一个costom 404处理页面,需要三个按钮(“返回上一页”;“返回首页”;“访问我们的帮助中心”),但是我遇到了一些麻烦,这是我想出来的以及我想解决的问题

My 404.HTML我的 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 %}

My forms.py我的表格.py

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

My routes.py(main function)我的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)

Here is what I got A picture of my 404 page这是我得到的 404 页面的图片

The button lays out perfectly as what I expected but the text on the button is incorrect, as it all prints out the text for the last button.该按钮完全符合我的预期,但按钮上的文本不正确,因为它全部打印出最后一个按钮的文本。 Another problem I faced is don't know how to modify my routes.py file so those three button could redirect the users to three identical page.我面临的另一个问题是不知道如何修改我的 routes.py 文件,以便这三个按钮可以将用户重定向到三个相同的页面。

Thanks谢谢

form.py表单.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 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 %}

Now, the first problem is solved.现在,第一个问题解决了。

@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)

And now, the second problem is solved.现在,第二个问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM