1

I can not figure out why my form will not return on the request. I have read multiple articles saying the the issue is with the 'name' attribute so I tried that to no success. Also I tried different ways of retrieving the form such as

request.form['form']

request.form.get('form')

request.form

Here is the form template

{%  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 %}

and here is the form route receiving the request

@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)

when i print(form)i get this in the terminal <__main__.RegisterForm object at 0x03840AF0> but it doesn't really tell me much. So another question I have is what is the best way to debug these issues?

Thanks in advance!

TJ Weems
  • 1,086
  • 3
  • 21
  • 37
  • 1
    try form = RegisterForm() – azdonald May 30 '19 at 20:42
  • @azdonald, thanks that solves half my issue. It seems that `form.validate()` always reads false so the only way I can write to the database is by removing that condition. Would you know what would cause that? – TJ Weems May 30 '19 at 20:47
  • To anyone that comes across the issue about `form.validate()`. This should solve your issue [stackoverflow article](https://stackoverflow.com/questions/20905188/flask-wtforms-validation-always-false). It has to do with csrf. Just need to add `{{ form.hidden_tag()}}`. problem solved. – TJ Weems May 30 '19 at 21:12

0 Answers0