WTforms表单未提交但没有输出验证错误 [英] WTforms form not submitting but outputs no validation errors

查看:53
本文介绍了WTforms表单未提交但没有输出验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 flask-uploads 运行并遇到一些障碍来上传文件.我将向您展示我的 flask 视图函数,html,并希望有人可以指出我所缺少的内容.

I'm trying to get file uploads with flask-uploads working and running in to some snags. I'll show you my flask view function, the html and hopefully someone can point out what I'm missing.

基本上,发生的事情是我提交了表单,并且它在 if request.method =='POST'和form.validate():签入视图功能时失败.它跳下来显示模板.wtforms不会踢我表格上的任何错误,因此我想知道为什么它无法通过if语句.

Basically what happens is that I submit the form and it fails the if request.method == 'POST' and form.validate(): check in the view function. It jumps down to display the template. wtforms isn't kicking me any errors on the form so I'm wondering why its failing that if statement.

我在看什么?

设置烧瓶上载:

# Flask-Uploads
photos = UploadSet('photos',  IMAGES)
configure_uploads(app, (photos))

查看:

def backend_uploadphoto():
    from Application import photos
    from Application.forms.backend import AddPhotoForm

    clients = Client.query.all()
    events = Event.query.order_by('date').all()

    form = AddPhotoForm(request.form, csrf_enabled=True)

    if request.method == 'POST' and form.validate():
        from uuid import uuid4

        uuid = uuid4()
        filename = '{0}.jpg'.format(uuid)

        photo = Photo(uid=uuid, client=request.form['client'], event=request.form['event'])

        photofile = photos.save(request.files.get('photo'), photo.filename)

        return redirect(url_for('backend'))

    return render_template('backend/addphoto.html', form=form, clients=clients, events=events)

表格:

class AddPhotoForm(Form):
    photo = FileField('Photo')
    client = IntegerField('Client:')
    event = IntegerField('Event:')

HTML:

<form action="{{url_for('backend_uploadphoto')}}" method="post">
        <p>
            {{form.client.label}}
            <select name="client">
                {% for client in clients %}
                <option value="{{client.id}}">{{client.fullname}}</option>
                {% endfor %}
            </select>
            {{form.client.errors}}
        </p>

        <p>
            {{form.event.label}}
            <select name="event">
                {% for event in events %}
                <option value="{{event.id}}">{{event.name}}</option>
                {% endfor %}
            </select>
            {{form.event.errors}}
        </p>

        <p><label for="photo">Photo:</label>{{form.photo}} <input type="submit" value="Upload"> {{form.photo.errors}}</p>
    </form>

推荐答案

您具有 csrf_enabled = True ,但是您的表单没有任何CSRF保护,因为您没有从 SecureForm继承.如果要启用CSRF,请阅读文档和更新您的表单定义.

You have csrf_enabled=True but your form doesn't have any CSRF protection since you aren't inheriting from SecureForm. If you want to enable CSRF, read the documentation and update your form definition.

如果这是意外的,则可以删除 csrf_enabled = True ,您的逻辑将按预期工作.

If this was unintended, you can remove csrf_enabled=True and your logic will work as expected.

要启用CSRF保护,请执行以下步骤:

To enable CSRF protection, there are a few steps:

  1. 继承自 SecureForm
  2. 在表单中创建 generate_csrf_token validate_csrf_token 方法.这些方法将生成唯一的密钥,并且在不验证时会引发错误.
  3. {{form.csrf_token}} 添加到模板中.
  1. Inherit from SecureForm
  2. Create the generate_csrf_token and validate_csrf_token methods in your form. These methods will generate a unique key and raise errors when it doesn't validate.
  3. Add {{ form.csrf_token }} to your template.

这篇关于WTforms表单未提交但没有输出验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆