I am building an application where I need to ask User its Location.For that, I am using SelectField of WTForm Flask. Here we ask the user to select its state from the dropdown. After using the selected state name, we populate district column. I have written the code but it's not able to change district column values after we change state name.
Form Code
class AddForm(Form):
state=SelectField('State',coerce=int,default=29)
district= SelectField("District",default=29)
submit = SubmitField('Submit')
View Code
@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)
Any help is appriciated. Thank You