Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if form.sf_baclient.data == 'py':
- form.sf_clientplan.choices = list2
- else:
- form.sf_clientplan.choices = list3
- <form action="{{ url_for('index') }}" method=post>
- {{ form.hidden_tag() }}
- <dl>
- {{ form.sf_baclient.label }}<br>
- {{ form.sf_baclient }}<br>
- {{ form.sf_clientplan.label }}<br>
- {{ form.sf_clientplan }}<br>
- {{ form.ff_pdf_upload.label }}<br>
- {{ form.ff_pdf_upload }}<br>
- </dl>
- {{ form.sb_submit }}
- </form>
- @app.route('/', methods=['GET', 'POST'])
- def index():
- form = MyForm(request.form)
- list1 = [('cpp', 'C++'), ('py', 'Python'), ('text', 'Plain Text')]
- list2 = [('1', 'One'), ('2', 'Two'), ('3', 'Three')]
- list3 = [('blue', 'Blue'), ('red', 'Red'), ('green', 'Green')]
- form.sf_baclient.choices = list1
- form.sf_baclient.choices.insert(0, ('', ''))
- form.sf_clientplan.choices = list3
- form.sf_clientplan.choices.insert(0, ('', ''))
- if request.method == 'GET':
- return render_template('home.html', form=form)
- elif request.method == 'POST':
- if form.sb_submit.data == True:
- message = form.sf_baclient.data
- # flash(message)
- return render_template('home.html', form=form, success=True)
- @app.route('/_parse_data', methods=['GET'])
- def parse_data():
- if request.method == "GET":
- # only need the id we grabbed in my case.
- id = request.args.get('b', 0)
- new_list = _call_to_db(id)
- # When returning data it has to be jsonify'ed and a list of tuples (id, value) to populate select fields.
- # Example: [('1', 'One'), ('2', 'Two'), ('3', 'Three')]
- return jsonify(new_list)
- $('#sf_baclient').on('change', function() {
- $("#sf_clientplan").empty();
- baclient_name = $("#sf_baclient option:selected").text();
- baclient_id = $("#sf_baclient option:selected").val();
- # Sending variables containing selection info to parse_data function in python.
- # jQuery's builtin ajax functions make things super easy
- # Calling that parse url and sending it arguments
- $.getJSON($SCRIPT_ROOT + '/_parse_data', {
- a: baclient_name,
- b: baclient_id
- # Function to get data returned from parse_data and populate the second select field.
- }, function(data) {
- # Using jQuery to populate new entries we got from flask.
- var options = $("#sf_clientplan");
- $.each(data, function() {
- options.append($("<option />").val(this).text(this));
- });
- });
- if 'py' in form.sf_baclient.data:
- form.sf_clientplan.choices = list2
- else:
- form.sf_clientplan.choices = list3
Add Comment
Please, Sign In to add comment