0

I'm currently working on my first python flask website. So I'm struggling with a basic task :-)

I have two comboboxes on my page (lets say comboboxes A (brands) and B (models)). When the website is loaded, A gets filled. B depends on A, which means that when I choose a value on A, B should get loaded accordingly. My problem is that I don't know how to call a function to load B. I would appreciate any advice, thanks.

#PYTHON
#load data for Combobox A
@app.route('/')
def home():
    connection = pypyodbc.connect('Driver={SQL Server};Server=localhost;Database=Demo')
    cursor = connection.cursor()
    cursor.execute("select Brand from [dbo].[CarBrands]")
    data = cursor.fetchall()
    brands = []
    for val in data:
        brands.append(val[0])
    return render_template("home.html",brands=brands)


#HTML
#Display A
#Load B according to A
{% extends "layout.html" %}
{% block content %}
  <div class="home">
    <h1>Demo</h1>
    <p>Test</p>
  </div>

   <select>
      {% for item in brands %}
      <option value={{item}}>{{item}}</option><br>
      {% endfor %}
  </select>

  <select>
      {% for item in models %}
      <option value={{item}}>{{item}}</option><br>
      {% endfor %}
  </select>

{% endblock %}
cegas
  • 2,823
  • 3
  • 16
  • 16
chrigi
  • 63
  • 1
  • 5
  • 1
    if you want it to be dynamic _(e.g. change every time box A updates)_ then you need to use javascript. i wrote an answer [**to this SO post**](http://stackoverflow.com/questions/41232105/populate-wtforms-select-field-using-value-selected-from-previous-field/41246506#41246506) which details everything you would need to accomplish this. – abigperson Mar 12 '17 at 18:16
  • thank you very much! that post solved my problem – chrigi Mar 12 '17 at 20:19

0 Answers0