使用 Flask-WTF/WTForms 在 Flask 中添加取消按钮

Phi*_*ser 5 html flask wtforms

我想添加一个返回上一页的取消按钮。我的代码是:forms.py:

from flask_wtf import Form
from wtforms import StringField, HiddenField, SubmitField
from wtforms.validators import DataRequired, Length, ValidationError


def _required(form, field):
    if not field.raw_data or not field.raw_data[0]:
        raise ValidationError('Field is required')

class OrganisationForm(Form):
    id = HiddenField('id', default="-1")
    name = StringField('name', validators=[DataRequired()])
    manager_name = StringField('manager_name')
    address = StringField('address', validators=[DataRequired()])
    city = StringField('city', validators=[DataRequired()])
    postal_code = StringField('postal_code', validators=[DataRequired(), Length(max=16)])
    province = StringField('province', validators=[Length(max=2, message="Can't exceed 2 characters")])
    country = StringField('country', validators=[DataRequired()])
    submit = SubmitField('Add')
    cancel = SubmitField('Cancel')
Run Code Online (Sandbox Code Playgroud)

和模板页面:

{% block content %}
<div class="content-section">
  {{ utils.flashed_messages() }}
  <div class="center">

      {% if add_orgnisation %}
                <h1>add an organisation</h1>
            {% else %}
                <h1>Edit an organisation</h1>
      {% endif %}
    <br/>
    <br/>
    {{ wtf.quick_form(form,novalidate=True) }}
  </div>
</div>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

视图,py

@orgs.route('/organisations/org_new')
@login_required
def org_new():
    add_orgnisation = True
    form = OrganisationForm()
    return render_template("organisations/organisation_form.html", form=form, title="New Organisation", edit=False, add_orgnisation=add_orgnisation)
Run Code Online (Sandbox Code Playgroud)

当我点击取消按钮时出现 405 错误:方法不允许。

我的错误在哪里?当我单击“取消”时,我应该添加什么才能返回上一页?

谢谢

Gre*_* Li 7

当我点击取消按钮时出现 405 错误:方法不允许。我的错误在哪里?

当您提交表单时,数据将作为 POST 请求发送(因为呈现的表单元素将使用<form method="post">)。但是,您的视图函数默认只允许 GET 请求,要接受 POST 请求,您必须指定methods如下参数:

@orgs.route('/organisations/org_new', methods=['GET', 'POST'])  # <--
@login_required
def org_new():
   # ...
Run Code Online (Sandbox Code Playgroud)

当我单击“取消”时,我应该添加什么才能返回上一页?

使用创建的任何字段都SubmitField将呈现为提交按钮 ( <input type="submit">),因此当您单击它时它将提交表单。

要让点击取消按钮时返回上一页,通常有两种方法可以实现:

1.在视图函数中捕获按钮提交

就像Ben的回答中的方法一样。您可以捕获提交,然后将用户重定向到上一页:

@orgs.route('/organisations/org_new', methods=['GET', 'POST'])
@login_required
def org_new():
    if request.method == 'POST':
        if form.cancel.data:  # if cancel button is clicked, the form.cancel.data will be True
            return redirect(url_for('previous_page_view_name'))
    # ...
Run Code Online (Sandbox Code Playgroud)

PS 由于您已经novalidate=True在 中进行了设置wtf.quick_form,因此无需在表单类中的按钮render_kw={'formnovalidate': True}上进行设置。cancel

2. 创建<a>按钮而不是cancel字段

您可以创建一个普通<a>元素作为取消按钮 ( class="btn btn-secondary") 并href用上一页的 URL 填充参数(这样您就不需要cancel在表单类中添加字段)。这样,您就不能使用wtf.quick_form(),而是需要使用 手动渲染每个字段wtf.form_field()

<form method="post">
    {{ wtf.form_field(form.id) }}
    {{ wtf.form_field(form.name) }}
    {{ wtf.form_field(form.manager_name) }}
    {{ wtf.form_field(form.address) }}
    ...
    {{ wtf.form_field(form.submit) }}
    <a href="{{ url_for('previous_page_view_name') }}" class="btn btn-secondary">Cancel</a>
</form>
Run Code Online (Sandbox Code Playgroud)


bal*_*ldy 0

不,我正在使用 TimeField 来强制捕获有效时间。我没有尝试将其更改为普通文本字段,因此使用上面的取消链接选项。不过,我的偏好仍然是有一个“取消”按钮。