gpt4 book ai didi

python - Flask,wtforms 回发空表格

转载 作者:行者123 更新时间:2023-11-28 19:10:36 25 4
gpt4 key购买 nike

我是新手。我想要的是显示值,当我单击提交按钮时,可以回发该值。然而,第一步已经完成,但它没有返回任何内容。我检查了 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 是,它运行良好:

Screenshot of the form

当我点击按钮时,表单数据变成空数据集:

{'trading': [{'trading_account': u'', 'code': u'', 'trading_stage': u'',     'submit': False, 'trading_mode': u''}, {'trading_account': u'', 'code': u'', 

但是,request.form 有所有的数据,这个过程有什么问题吗?

最佳答案

也许问题在于 request.form 是 ImmutableMultiDict like ('code', u'600844'), ('code', u'600848'), ('code', u '601888'),不是由form构造的(request.form)

关于python - Flask,wtforms 回发空表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40828296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com