[FIXED] How can i populate a form from a Dropdown Control Selector using SQL and Flask?


I’m stuck trying to populate a HTML form, selecting the company from a dropdown, similar result to this, in SQL and Flask.

I have no clue which way to go, to bring a value from <option> in HTML back to Flask file.

Here are some samples of my code.

Flask Sample:

@app.route("/profile" ,  methods=['GET', 'POST'])
def profile():

if request.method == "GET":
    querieCompanies = db.execute("SELECT name FROM companies ORDER BY name ASC")
    querieAll = db.execute("SELECT * FROM companies WHERE id = ?", ??WHATGOESHERE??).fetchall()

return render_template("profile.html", querieCompanies=querieCompanies, querieAll=querieAll)

HTML Sample:

{% extends "layout.html" %}

{% block main %}

<form name="listprofile" method="get">
    <div class="mb-3">
        <h5>Select company</h5>
        <select class="mx-auto w-auto" name="dropdownmenu">
            <option disabled selected>Companies</option>
            {% for companies in querieCompanies %}
                <option value="{{ companies[0] }}">{{ companies[0] }}</option>
            {% endfor %}
<div class="container">

<div class="row">
    <div class="col mb-3 p-1 d-inline-bloc">
        {% for data in querieAll %}
            ??WHAT GOES HERE??
        {% endfor %}

Also, it seems i can’t populate <input>, i’m able to populate a <textbox>, is there another solution?


Use request.args for getting data from get-method form

if you using sqlalchemy, then use colon-params in db.execute()

from flask import request
querieAll = db.execute("SELECT * FROM companies WHERE id = :value", {
    'value': request.args.get('dropdownmenu')
  1. In template for-loop, data is a tuple with companies columns data.

Answered By – sergelab

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published