Reputation: 2244
I am fairly new to Flask, and am working on a simple website to do calculations (noting too difficult, just simple additions, division). I am currently using an Flask-WTF and creating a form that uses IntegerField's to collect information. Here is my forms.py:
from flask.ext.wtf import Form
from wtforms import IntegerField, SubmitField, validators, ValidationError
class investment(Form):
A = IntegerField("Enter value A: ")
B = IntegerField("Enter value B: ")
submit = SubmitField("Calculate")
Here is my routes.py:
@app.route('/', methods=['GET', 'POST'])
def home():
form = investment()
if request.method == 'POST':
return A + B
elif request.method == 'GET':
return render_template('home.html', form=form)
Anyone know how I can add A and B together after the user has filled out the form and hit submit? Thanks.
Upvotes: 1
Views: 5154
Reputation: 14939
You should give the documentation more time -
@app.route('/', methods=['GET', 'POST'])
def home():
form = investment(request.form)
if request.method == 'POST' and form.validate():
sumtn = form.A.data + form.B.data
# Do what you want with sum
elif request.method == 'GET':
return render_template('home.html', form=form)
Upvotes: 3
Reputation: 76753
There might be multiple problems with your code, but an obvious problem is
@app.route('/', methods=['GET', 'POST'])
def home():
form = investment()
if request.method == 'POST':
return A + B # <--- where do A and B come from? They're never defined!
elif request.method == 'GET':
return render_template('home.html', form=form)
I am not sure how wtforms/flask-wtf expects you to get to them for sure, but I think it's something like
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
form = investment(request.POST)
return form.A + form.B
elif request.method == 'GET':
form = investment()
return render_template('home.html', form=form)
You'll have to check out the documentation to be sure
Upvotes: 0