DragLag DragLag - 3 months ago 19
Python Question

Flask does not take WTForms input

I'm tring to create an app with flask with WTForms.

In the controller.py i have:

@mod_private.route('/portfolio/', methods=['GET', 'POST'])
@login_required
def portfolio():
print "in portfolio" # I read this
form = CreateCoinsForm(request.form)
if request.method == 'POST' and form.validate_on_submit():
print form.coins.data #I cannot take this value
return render_template("private/portfolio.html",form=form)
return render_template("private/portfolio.html",form=form)


in the forms.py:

class CreateCoinsForm(Form):
coins = IntegerField('coins',
[DataRequired('num required'),
NumberRange(min=0, max=10)])


and the template

<form method="post" action="/private/portfolio/" accept-charset="UTF-8" role="form">
<p> {{ form.coins }}</p>
<p><input type=submit value=Generate>
</form>


my problem, as i wrote in the code is that I cannot retrieve the string inserted in the template.

Answer

Your problem suggests that you are using the built-in CSRF protection on your form, and your form actually isn't validating because you haven't included the CSRF token.

Try adjusting your template like so:

<form method="post" action="/private/portfolio/" accept-charset="UTF-8" role="form">
   {{ form.hidden_tag() }}
           <p> {{ form.coins }}</p>
        <p><input type=submit value=Generate>
    </form>