【问题标题】:Flask - WTF - Input Required Validator - Restrict the request from being posted (Stay on page)Flask - WTF - Input Required Validator - 限制发布请求(留在页面上)
【发布时间】:2016-03-15 13:25:04
【问题描述】:

我正在学习如何在一个新的应用程序概念证明中一起使用 WTForms 和 Flask。

我有一个表格。目标是要求姓氏至少包含 3 个字符。

class PersonByNameForm(Form):
    first_name = StringField('First Name', filters=none_filter)
    last_name = StringField(validators=[InputRequired('Enter a Last Name'), Length(min=3)])
    submit = SubmitField('SUBMIT')

我这样渲染表单

@app.route('/', methods=['GET'])
def index():
    if request.method == 'GET':
        form = PersonByNameForm()
        return render_template('front_page.html', form=form)

html

 <form action="person_profiles" method="post">
                {{form.hidden_tag()}}
                {{form.first_name.label}}
                {{form.first_name}}
                {{form.last_name.label}}
                {{form.last_name}}
                {{form.submit}}
            </form>

表单本身将数据发布到

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

    if request.method == 'GET':
        # This is just place holder but this view will have copy of the form
        form = PersonByNameForm()
        form2 = FindPersonForm()
        return render_template('person_profile.html', context=[], form=form, form2=form2)

    else:

        form = PersonProfileForm(request.form)

        if form.validate_on_submit():
            query = Session.query(schema.Person)
            first_name = form.first_name.data
            last_name = form.last_name.data
            print(first_name, last_name)
            if first_name:
                query = query.filter(schema.Person.first_name.contains(first_name))
            if last_name:
                query = query.filter(schema.Person.last_name.contains(last_name))

            return render_template('person_profile.html', context=query.all())
        else:
            print(form.errors)
            error = form.errors
            flash_errors(form, 'test')
            return render_template('person_profile.html', error=error, form=form)

表单将正确validate_on_submit(),并进入else块打印{'last_name': ['Enter a Last Name']}的form.error

我遇到的问题是,我不想实际渲染模板(它就在那里,因为我现在必须返回响应)。并且屏幕上没有闪烁任何错误。

如何限制导航直到 x 个字符出现在框中?如果它没有验证并闪烁消息?

感谢阅读

【问题讨论】:

  • 投反对票的人能否评论一下为什么?

标签: python flask wtforms flask-wtforms


【解决方案1】:

我有点不清楚你的问题是什么。

表单将正确验证_on_submit()

所以输入的字符串至少有 3 个字符长,对吧?

我遇到的问题是,我不想实际渲染模板(它就在那里,因为我现在必须返回响应)。并且屏幕上没有任何错误提示。

要闪烁错误消息,请在调用 flash 后,使用已输入的值再次呈现模板。您必须渲染一些内容以包含 Flash 消息。

如何限制导航直到 x 个字符出现在框中?如果它没有验证并闪烁消息?

您是否要在名称中至少包含 3 个字符之前阻止提交表单(客户端)?

这可以通过在模板中调用字段时传递max_length=3 来实现:

{{form.last_name(max_length=3)}}

很明显,这里的问题是您直接在模板中传递了 3。 AFAIK,WTForms 不会自动为您执行此操作。

【讨论】:

  • 您好杰罗姆,感谢您的意见。 properly 我的意思是 `validate_on_submit 在没有数据的情况下提交是错误的。 (如预期的那样)。但是,我似乎无法在屏幕上闪烁任何内容
  • 在flash_errors之后,需要渲染一个模板。事实上,当您遇到 else 案例时,您没有收到诸如“函数不返回任何内容”之类的错误,我很惊讶。
  • 我愿意,我只是将代码更新为当前状态。它不闪烁,但我可以发送error 变量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 2019-02-13
  • 1970-01-01
相关资源
最近更新 更多