简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM