Home > Enterprise >  How can i populate a form from a Dropdown Control Selector using SQL and Flask?
How can i populate a form from a Dropdown Control Selector using SQL and Flask?

Time:09-11

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 >
        <h5>Select company</h5>
        <select  name="dropdownmenu">
            <option disabled selected>Companies</option>
            {% for companies in querieCompanies %}
                <option value="{{ companies[0] }}">{{ companies[0] }}</option>
            {% endfor %}
        </select>
    </div>
<div >

<div >
    <div >
        <h5>Name</h5>
        {% 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?

CodePudding user response:

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')
}).fetchall()
  1. In template for-loop, data is a tuple with companies columns data.
  • Related