我是一名新学员。我想要的是显示值,当我点击提交按钮时,可以回发值。但是,第一步已经完成,但它没有发布任何内容。我检查了request.form
有数据。以下是我的代码:
from flask_wtf import Form
from wtforms import TextField,SelectField,FieldList,SubmitField,FormField # 不能引入* 否则会顶掉前面的Form,导致validate方法不能用
from wtforms.validators import DataRequired
from flask import Flask,render_template,request,redirect,url_for,flash
import pandas as pd
import wtforms_json
import json
wtforms_json.init()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'you-will-never-guess'
dat = pd.read_csv('trading_config.csv',dtype={'code': str},encoding='gbk')
js = dat.to_dict(orient = 'records')
#js = wtforms_json.flatten_json(json.loads(js))
class TradingForm(Form):
code = TextField('code')
trading_stage = SelectField('trading_stage',
choices=[('wait','wait'),('signal','signal'),
('exit','exit'), ('exclude','exclude')],default = '' )
trading_mode = SelectField('trading_mode',
choices=[('-1','-1'),('1','1'),('2','2')],default = '')
trading_account = TextField('trading_account')
submit = SubmitField("Send")
class ListForm(Form):
trading = FieldList(FormField(TradingForm), min_entries = 5)
@app.route('/success', methods=('GET', 'POST'))
def success():
return render_template('success.html')
@app.route('/', methods=('GET', 'POST'))
def submit():
form = ListForm()
trading_form = TradingForm()
if 'update' in request.form and request.method =='POST':
if form.validate == True:
flash(form.errors)
if form.validate_on_submit() == False:
flash(form.data)
flash(request.form)
flash(form.errors)
else:
print form
return redirect (url_for('success'))
#return render_template('hello.html', form=form,trading_form = TradingForm())
for item in js:
trading_form = TradingForm(**item)
form.trading.append_entry(trading_form)
return render_template('hello.html', form=form)
if __name__ == '__main__':
app.run(debug = True)
而html
是:
{% for message in get_flashed_messages() %}
<div class="flash">{{ message }}</div>
{% endfor %}
<form method="POST" action="http://localhost:5000/">
{{ form.hidden_tag() }}
{{ form.csrf_token }}
{{ form.trading.csrf_token }}
<fieldset>
<table>
<tr>
<th> 股码 </th> <th>交易状态 </th>
<th> 策略模式 </th> <th>交易账户 </th>
</tr>
{% for l in form.trading %}
<tr>
<td>{{ l.code.data}} </td> <td>{{ l.trading_stage.data}} </td>
<td>{{ l.trading_mode.data}}</td> <td>{{ l.trading_account.data}} </td>
</tr>
{% endfor %}
</table>
<input type="submit" name="update" value="update">
</fieldset>
</form>
Get
是,它运作良好:
当我点击按钮时,表单数据变为空数据集:
{'trading': [{'trading_account': u'', 'code': u'', 'trading_stage': u'', 'submit': False, 'trading_mode': u''}, {'trading_account': u'', 'code': u'',
但是,request.form
包含了所有数据,这个过程有什么问题?
答案 0 :(得分:0)
也许问题是request.form
是ImmutableMultiDict(''code',u'600844'),('code',u'600848'),('code',u'601888'),不是由表单构建的(request.form)