尝试创建一种使用WTFforms进行计算的方法,例如在输入两个值后返回加法运算
如何在表单中获取两个值并进行计算并返回一个值?谢谢
请查看代码
from flask import Flask, render_template
from flask_wtf import Form
from wtforms import TextField, PasswordField
from wtforms.validators import InputRequired
from bs4 import BeautifulSoup as soup
import urllib.request
import pandas as pd
import pandas_datareader as pdr
from pandas import ExcelWriter
app = Flask(__name__)
app.config['SECRET_KEY'] = 'DontTellAnyone'
class LoginForm(Form):
TextField = TextField('Stock', validators=[InputRequired()])
password = PasswordField('password', validators=[InputRequired()])
@app.route('/', methods=['GET', 'POST'])
def index():
form = LoginForm()
list = ['TextField']
list_value = list[0]
if form.validate_on_submit():
a = print ('a2')
#return a
return render_template('index.html', form=form)
if __name__ == '__main__':
app.run(debug=True)
#index.html
{% from "_formhelpers.html" import render_field %}
<html>
<head>
<title>WTForms</title>
</head>
<body>
<form method="POST" action="/">
<dl>
{{ form.csrf_token }}
{{ render_field(form.TextField) }}
{{ render_field(form.password) }}
<input type="submit" value="Earnings">
</dl>
</form>
</body>
</html>
答案 0 :(得分:1)
您可以在Form
上定义一个方法,该方法可以访问自己的属性,就像Python中的任何对象一样。
例如:
from wtforms import Form, IntegerField
class MyForm(Form):
a = IntegerField('A')
b = IntegerField('B')
def a_plus_b(self):
return self.a.data + self.b.data
if __name__ == "__main__":
form = MyForm(a=1, b=2)
print(form.a_plus_b()) # 3