wtforms Flask

时间:2017-02-06 16:55:58

标签: flask flask-wtforms

我正在构建一个应用程序,我需要向User询问它的位置。为此,我使用的是WTForm Flask的SelectField。在这里,我们要求用户从下拉列表中选择其状态。使用选定的州名后,我们填充区列。我编写了代码但是在更改状态名称后无法更改分区列值。 Here is a screenshot

表格代码

class AddForm(Form):    
     state=SelectField('State',coerce=int,default=29)
     district= SelectField("District",default=29) 
     submit = SubmitField('Submit')

查看代码

@app.route('/add', methods=['GET', 'POST'])
def add():
     form = AddForm()
     location_dao = DAOFactory.getDAO('LocationDAO')
     states=location_dao.getStates();
     form.state.choices=states
     form.district.choices=location_dao.getDistricts(form.state.data);
     if form.validate_on_submit():
         data={}
         data['state']=form.state.data
         data['district']=form.district.data
         success = location_dao.addLocation(data)
         if success is not None:
             msg='done!!!'
         else:
             msg='Something went wrong'
         flash(msg)
         return redirect(url_for('main.welcome'))
   return render_template('location/add.html',form = form)

任何帮助都是适当的。谢谢

0 个答案:

没有答案