Flask/WTForms-发布请求不发送输入 [英] Flask/WTForms - post request doesnt send input

查看:77
本文介绍了Flask/WTForms-发布请求不发送输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么我的帖子请求的正文为无"吗? 我正在将Flask与WTForms一起使用.

does somebody know why the body of my post request is None? I am using Flask with WTForms.

我的forms.py

My forms.py

class SignupForm(Form):
    username = StringField('Username')
    password = PasswordField('Password')
    email = StringField('Email')
    submit = SubmitField('Create account')

我的route.py

My route.py

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

    if request.method == 'POST':
        app.logger.info(form.data)
        return redirect(url_for('signup'))
    elif request.method == 'GET':
        return render_template('signup.html', form=form)

我的signup.html

My signup.html

<form action="{{ url_for('signup') }}" method="POST">
    {{ form.username.label }}
    {{ form.username }}

    {{ form.password.label }}
    {{ form.password }}

    {{ form.email.label }}
    {{ form.email }}

    {{ form.submit }}
</form>

我的route.py中的

app.logger.info(form.data)返回了我

app.logger.info(form.data) in my routes.py returns me

{'password': None, 'submit': False, 'username': None, 'email': None}

即使我给它提供了用户名等,我也按下了提交按钮

even though i give it a username etc. and i pressed the submit button

推荐答案

每次调用singup时,您都会创建一个空表格.您应该使用请求中的数据(如果有的话)填充它.

Every time singup is called you create an empty form. You should populate it with the data from the request(if present).

@app.route('/signup', methods=['GET', 'POST'])
def signup():
    form = SignupForm(request.form)
    if request.method == 'POST':
        # maybe call form.validate()
        app.logger.info(form.data)
        return redirect(url_for('signup'))
    elif request.method == 'GET':
        return render_template('signup.html', form=form)

WTForm类似于模型定义.它与Flask请求没有直接耦合,但是它知道如何从请求中填充其字段.

WTForm is like a model definition. It's not directly coupled with Flask request, but it knows how to populate its fields from the request.

这篇关于Flask/WTForms-发布请求不发送输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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