尝试预填充表单时,'str'对象不可调用

问题描述 投票:0回答:1

我正在尝试实现一个用于更新以前由用户发布的信息的表单。不幸的是,我得到一个TypeError:'str'对象不可调用。

我在网上寻找解决方案,但找不到任何 - 我的猜测是错误与我试图预先填充选择字段的值这一事实有关,但我可能错了。

views.朋友

@users.route('/<int:query_id>/update', methods=['GET', 'POST'])
@login_required
def update_query(query_id):
   query = Model.query.get_or_404(query_id)

   if query.author != current_user:
    abort(403)

   form = QueryForm()

   if form.validate_on_submit():
       query.property_type = form.property_type.data
       query.property_type_details = form.property_type_details.data

       db.session.commit()

       return redirect(url_for('users.user_profile', username=current_user.username))

   elif request.method == 'GET':
       form.property_type = query.property_type
       form.property_type_details = query.property_type_details

   return render_template('users/update-query.html', form=form)

forms.朋友

class QueryForm(FlaskForm):
   submit = SubmitField('Send')

   property_type = SelectField(u'Type', choices=[('House', 'House'), ('Apartment', 'Apartment')])
   property_type_details = SelectField(u'Detail', choices=[('Something', 'Something'),('SomethingElse', 'SomethingElse')])

模板

<form method='POST' class="query-form" action="" enctype="multipart/form-data">
      {{form.hidden_tag()}}

       <h4 class="text-center">Info</h4>

          <div class="form-row">
             <div class="form-group col-md-4">
                {{form.property_type.label}}
                {{form.property_type(class="form-control")}} 
              </div>

              <div class="form-group col-md-4">
                {{form.property_type_details.label}}
                {{form.property_type_details(class="form-control")}}
              </div>
          </div>
</form>

最近的电话和错误

File "/Users/1/Desktop/Code/Project/Name/main/templates/users/update-query.html", line 33, in block "content"
{{form.property_type_details(class="form-control")}}

TypeError: 'str' object is not callable
flask wtforms
1个回答
0
投票

预填充两个表单字段意味着为两个字段实例设置data属性。 1

目前,两个字段的实例都被从数据库中检索的值覆盖。

当更新如下时预填充两个字段的块解决了在模板中预先填充两个字段的错误。

elif request.method == 'GET':
       form.property_type.data = query.property_type
       form.property_type_details.data = query.property_type_details
© www.soinside.com 2019 - 2024. All rights reserved.