简体   繁体   English

将wtforms 0.6升级到1.0 Form TextField数据无

[英]Upgrade wtforms 0.6 to 1.0 Form TextField data None

I have a flask/wtforms application and I am upgrading from wtforms 0.6.ish to 1.0.5. 我有一个flask / wtforms应用程序,我正在从wtforms 0.6.ish升级到1.0.5。 The development box was ubuntu which uses wtforms 0.6, while the production will be an amazon ami, which uses wtforms 1.0.5. 开发箱是使用wtforms 0.6的ubuntu,而产品将是使用wtforms 1.0.5的亚马逊ami。

For anybody's benefit, the following changes so far are: 为了所有人的利益,到目前为止,进行了以下更改:

from: 从:

from flask.ext.wtf import Form, TextField
from flask.ext.wtf import Required

to: 至:

from wtforms import Form,  TextField, validators

from: 从:

def index():
  form=SubmitForm()
  if form.validate_on_submit():
    return render_template('js.html',ht=form.ht.data)

to: 至:

def index():
  form=SubmitForm
  if request.method == 'POST' and form.validate():
    return render_template('js.html',ht=form.ht.data)

also: 也:

class SubmitForm(Form):
    ht = TextField('ht', validators = [Required()])

class SubmitForm(Form):
   ht = TextField('ht',  [validators.Required()])

The trouble is that in wtforms 1.0.5 the form variable 'ht' never set. 问题在于,在wtforms 1.0.5中,从未设置过形式变量“ ht”。 If I attempt to print it to the console, it's 'None'. 如果我尝试将其打印到控制台,则为“无”。 I haven't made any changes to the template. 我尚未对模板进行任何更改。 I can set a default value in the class, but it won't get replaced by what the user enters on the form. 我可以在该类中设置一个默认值,但不会被用户在表单上输入的内容代替。

In your new controller you don't actually create an instance of your submit form (you are missing the parentheses). 在新的控制器中,您实际上并没有创建提交表单的实例(缺少括号)。 Also, you switched from the Flask-WTForms extension to plain WTForms. 同样,您从Flask-WTForms扩展名切换到普通WTForms。 Flask-WTForms automatically uses request.form so you don't have to - if you are going to use vanilla WTForms, you will need to pass request.form to your constructor. Flask-WTForms自动使用request.form因此您不必-如果要使用香草WTForms,则需要将request.form传递给构造函数。

# You have
form=SubmitForm

# It should be
form = SubmitForm(request.form)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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