request.form即使具有name属性也不会返回表单Python Flask

时间:2019-05-30 20:36:33

标签: python forms flask

我无法弄清楚为什么我的表格不会在请求中返回。我已经阅读了多篇文章,说问题出在“名称”属性上,所以我尝试没有成功。我还尝试了其他方式来检索表格,例如

request.form['form']

request.form.get('form')

request.form

这是表单模板

{%  block body %}
    <div class="app-container">
        <h2 class="page-header">Register Form</h2>
        {% from "includes/_formhelpers.html" import render_field %}
        <form action="" method="POST" name="form">
            <div class="form-group">
                {{render_field(form.name, class_="form-input")}}
            </div>
            <div class="form-group">
                {{render_field(form.email, class_="form-input")}}
            </div>
            <div class="form-group">
                {{render_field(form.username, class_="form-input")}}
            </div>
            <div class="form-group">
                {{render_field(form.password, class_="form-input")}}
            </div>
            <div class="form-group">
                {{render_field(form.confirm, class_="form-input")}}
            </div>
            <div class="form-group">
                <input type="submit" class="form-btn" value="Submit" name="button">
            </div>
        </form>
    </div>
{% endblock %}

这是接收请求的表单路径

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm(request.form.get('form'))
    print(form)
    if request.method == 'POST' and form.validate():
        name = form.name.data
        email = form.email.data
        username = form.username.data
        password = sha256_crypt.encrypt(str(form.password.data))
        # set mysql cursor
        cur = mysql.connection.cursor()
        # insert statement for data to be commited
        cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))
        # commit to db
        mysql.connection.commit()
        # close connection
        cur.close()
        flash('You are now registered, please login', 'success')
        redirect(url_for('register'))
    return render_template('register.html', form=form)

当我print(form)在终端<__main__.RegisterForm object at 0x03840AF0>上收到此消息时,但这并不能告诉我很多。所以我还有一个问题是调试这些问题的最佳方法是什么?

谢谢!

0 个答案:

没有答案