flask - Dynamic routing from form data -


i new flask having issue creating dynamic url form data. value of selectfield of wtforms. code follows

my form looks this

from flask_wtf import form wtforms import selectfield wtforms.fields.html5 import datefield   class selecteventform(form):     sports = selectfield(u'select sport')     start_after_date = datefield('starts after date')     start_before_date = datefield('starts before date') 

my controller has following code

@app.route('/event', methods=['get', 'post']) def event():     form = selecteventform(request.form)     sports = betfair_client.call_rest_api('listeventtypes/', {"filter": {}})     form.sports.choices = []     sport in sports:        key in sport:            form.sports.choices.append((key, sport[key]))    return render_template('events.html', form=form)   @app.route('/event/<sports>', methods=['get', 'post']) def event_select(sports):     #print request.form     #print request.form.get('sports')     return render_template('events_two.html') 

the form in html follows

<form  class="form-style-7" action="{{ url_for('event_select',     sports=form.sports.sport) }}" method="post">   <ul>     <li name="sport">       {{ form.sports.label}} {{ form.sports }}     </li>      <li>       {{ form.start_after_date.label }} {{ form.start_after_date }}     </li>     <li>       {{ form.start_before_date.label }} {{ form.start_before_date }}     </li>      &nbsp;     &nbsp;    <li>     <input type="submit" value="next">    </li> </ul> </form> 

what do, se value selectfield generate url. have been stuck on time. form , drop down list display correctly

thanks

i'd change form post it's generating page, , deal logic there. change

{{ url_for('event_select', sports=form.sports.sport) }} 

to just:

{{ url_for('event') }} 

then adjust event method like:

from flask import redirect  @app.route('/event', methods=['get', 'post']) def event():     form = selecteventform(request.form)      if form.validate_on_submit():         chosen_sport = form.sports.data         return redirect(url_for('event_select', sports=chosen_sport))      sports = betfair_client.call_rest_api('listeventtypes/', {"filter": {}})     form.sports.choices = []     sport in sports:        key in sport:            form.sports.choices.append((key, sport[key]))    return render_template('events.html', form=form) 

all we're doing grabbing if it's submitted, grabbing chosen sport form element, , using redirect user event_select method chosen sport argument.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -