尝试预填写表单时,无法调用“ str”对象

时间:2019-02-18 18:27:07

标签: flask wtforms

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

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

views.py

@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.py

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

1 个答案:

答案 0 :(得分:0)

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

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

按以下方式更新时,预填充两个字段的块将呈现两个字段预填充到模板中,从而解决了错误。

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