Guest User

Untitled

a guest
Feb 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. if form.sf_baclient.data == 'py':
  2. form.sf_clientplan.choices = list2
  3. else:
  4. form.sf_clientplan.choices = list3
  5.  
  6. <form action="{{ url_for('index') }}" method=post>
  7. {{ form.hidden_tag() }}
  8. <dl>
  9. {{ form.sf_baclient.label }}<br>
  10. {{ form.sf_baclient }}<br>
  11.  
  12. {{ form.sf_clientplan.label }}<br>
  13. {{ form.sf_clientplan }}<br>
  14.  
  15. {{ form.ff_pdf_upload.label }}<br>
  16. {{ form.ff_pdf_upload }}<br>
  17.  
  18. </dl>
  19. {{ form.sb_submit }}
  20. </form>
  21.  
  22. @app.route('/', methods=['GET', 'POST'])
  23. def index():
  24.  
  25. form = MyForm(request.form)
  26.  
  27. list1 = [('cpp', 'C++'), ('py', 'Python'), ('text', 'Plain Text')]
  28. list2 = [('1', 'One'), ('2', 'Two'), ('3', 'Three')]
  29. list3 = [('blue', 'Blue'), ('red', 'Red'), ('green', 'Green')]
  30.  
  31. form.sf_baclient.choices = list1
  32. form.sf_baclient.choices.insert(0, ('', ''))
  33.  
  34. form.sf_clientplan.choices = list3
  35. form.sf_clientplan.choices.insert(0, ('', ''))
  36.  
  37. if request.method == 'GET':
  38. return render_template('home.html', form=form)
  39.  
  40. elif request.method == 'POST':
  41. if form.sb_submit.data == True:
  42. message = form.sf_baclient.data
  43. # flash(message)
  44.  
  45. return render_template('home.html', form=form, success=True)
  46.  
  47. @app.route('/_parse_data', methods=['GET'])
  48. def parse_data():
  49. if request.method == "GET":
  50.  
  51. # only need the id we grabbed in my case.
  52. id = request.args.get('b', 0)
  53. new_list = _call_to_db(id)
  54.  
  55. # When returning data it has to be jsonify'ed and a list of tuples (id, value) to populate select fields.
  56. # Example: [('1', 'One'), ('2', 'Two'), ('3', 'Three')]
  57.  
  58. return jsonify(new_list)
  59.  
  60. $('#sf_baclient').on('change', function() {
  61.  
  62. $("#sf_clientplan").empty();
  63.  
  64. baclient_name = $("#sf_baclient option:selected").text();
  65. baclient_id = $("#sf_baclient option:selected").val();
  66.  
  67. # Sending variables containing selection info to parse_data function in python.
  68. # jQuery's builtin ajax functions make things super easy
  69.  
  70. # Calling that parse url and sending it arguments
  71. $.getJSON($SCRIPT_ROOT + '/_parse_data', {
  72. a: baclient_name,
  73. b: baclient_id
  74.  
  75. # Function to get data returned from parse_data and populate the second select field.
  76. }, function(data) {
  77. # Using jQuery to populate new entries we got from flask.
  78. var options = $("#sf_clientplan");
  79. $.each(data, function() {
  80. options.append($("<option />").val(this).text(this));
  81. });
  82.  
  83. });
  84.  
  85. if 'py' in form.sf_baclient.data:
  86. form.sf_clientplan.choices = list2
  87. else:
  88. form.sf_clientplan.choices = list3
Add Comment
Please, Sign In to add comment