Flask WTForms用变量自动填充StringField [英] Flask WTForms autofill StringField with variable
问题描述
我有一个表单,希望自动使用上一页中接收到的信息来填充某些字段,但是如果他们想对其进行调整,则需要将其更改.我正在为自己的SelectField使用动态创建的列表,但是添加StringField失败.请参阅下面的代码:
I have a form that I want to automatically fill some of the fields with information received on a previous page, but it needs to be changeable if they want to adjust it. I am using a dynamically created list for my SelectField that works, but adding the StringField has been unsuccessful. See my code below:
forms.py
class get_vals(var):
...
typs = var.p_typs
p_name = var.p_name
return typs,p_name
class NewForm(FlaskForm):
name = StringField('Name', validators=[DataRequired()])
typ1 = SelectField('Type', validators=[Optional()])
def __init__(self,var):
super(NewForm,self).__init__()
typs,p_name = get_vals(var)
self.typ1.choices = typs
self.name.default = p_name
使用:
self.name.default
将我的姓名留空.如果我使用:
leaves my name form blank. If I use:
self.name.data
它将正确的信息放入表格中,但是您不能更改值,并且必须使用其中的任何内容,这不是一个选择.
it puts the correct information in the form, however you cannot change the values and must use whatever it places in, which is not an option.
...
<form action="" method="post">
{{ form.hidden_tag() }}
<p>
{{ form.name.label }}
{{ form.name(size=24) }}
{{ form.typ1.label }}
{{ form.typ1() }}
</p>
...
有没有一种方法可以设置变量的默认值并使它可变?谢谢
Is there a way to set the default value which a variable and have it changeable? Thanks
更新 如果我在运行获取名称变量的Form后将字段设置为更新为None,则可以无错误地更改变量.但是,它不会覆盖form.name.data值
UPDATE If I set the field I am trying to update to None after running the Form that gets the name variable, I am able to change the variable without error. However, it won't overwrite the form.name.data value
form = NewForm(var)
var.name == None
if form.validate_on_submit():
var.name = form.name.data
这会运行(在表单中显示正确的变量并且是可调整的),但是在提交表单时不会覆盖该值,我该如何覆盖该值?
This runs (displays correct variable in form and is adjustable) but doesn't overwrite the value when you submit the form, how can I overwrite the value?
推荐答案
您可以通过传递 FlaskForm
的formdata
参数.
You can set initial values for fields by passing a MultiDict
* as FlaskForm
's formdata
argument.
from werkzeug.datastructures import MultiDict
form = NewForm(formdata=MultiDict({'name': 'Foo'}))
这将在呈现表单时将name
输入的值设置为"Foo",覆盖该字段的默认值.但是,您不希望在将表单回发到服务器时覆盖这些值,因此您需要在处理程序中检查request方法:
This will set the value of the name
input to 'Foo' when the form is rendered, overriding the default value for the field. However you don't want to override the values when the form is posted back to the server, so you need to check the request method in your handler:
from flask import render_template, request
from werkzeug.datastructures import MultiDict
@app.route('/', methods=['GET', 'POST'])
def hello():
if request.method == 'GET':
form = NewForm(formdata=MultiDict({'name': 'foo'}))
else:
form = NewForm()
if form.validate_on_submit():
# do stuff
return render_template(template, form=form)
* 要求使用MultiDict,因为在HTML表单中,可能有多个具有相同name
属性的输入,并且MultiDict具有处理值列表以及单个值(如a)的方法.正常dict
.
* A MultiDict is required because in an HTML form there may be multiple inputs with the same name
attribute, and a MultiDict has methods that handle lists of values as well as single values like a normal dict
.
这篇关于Flask WTForms用变量自动填充StringField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!