0

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. Here is a screenshot

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

prem
  • 47
  • 2
  • 7
  • To change elements dynamically you need to use Javascript/jQuery... Flask only executes on the server (e.g. before the page renders and in response to HTTP/XHR requests). See the example here: http://stackoverflow.com/questions/41232105/populate-wtforms-select-field-using-value-selected-from-previous-field/41246506#41246506 – abigperson Feb 06 '17 at 20:18
  • Thank you very much. It worked. – prem Feb 07 '17 at 10:35

0 Answers0