Python Forum
How do I fetch values from db to Select Options using Flask?
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I fetch values from db to Select Options using Flask?
#1
Hi Folks,

I am creating a web app and I am creating a edit page. I want to fetch data in Select > Option html tag with pre selected value from MySql.
For ex:
Options are:

option1
option2
option3


Now let suppose user have selected 2nd option, i.e option2
>> These options are available on a MySql table>Column named user_table.name, also selected option are available on other mysql table column named assign.assigned_to.

I don't how to do that. If someone can provide me code or guide me regarding this, will be great for me.

Thanks.
Reply
#2
you can use SQL download one of the packages here: https://pypi.python.org/pypi?%3Aaction=s...mit=search
use the weight column when evaluating the package.  The weight is based on several factors, like, how old the code is used, how reciently
downloaded, or updated, and number of downloads, etc.

I can't suggest one over the other because I don't use MySQL.
Reply
#3
Let's start by writing a little pretend sql to get the data:
select
    user.Name as Label,
    case when assigned.id is null then 0 else 1 end as Selected
from user_table as user
    left join assign as assigned on assigned.user_id = user.id
Which would get you a list of dicts, similar to this:
options = [
{"Label": "Joe", "Selected": False},
{"Label": "Steve", "Selected": True}
]
Which you could then turn into a dropdown in the jenja template...
<select>
{% for option in options %}
<option {% if option.Selected %}selected="selected"{% endif %}>{{ option.Label }}</option>
{% endfor %}
</select>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using yt-dlp options in Python Pavel_47 8 14,502 Jun-29-2022, 12:52 PM
Last Post: Pavel_47
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,619 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Flask and select element GrahamL 6 5,975 Nov-18-2020, 11:50 AM
Last Post: Anarab
  getting options from a html form pgoosen 5 3,259 Jul-03-2019, 06:07 PM
Last Post: nilamo
  How to fetch latitude,longitude from location and save them separately in db(Django2) PrateekG 0 2,632 Jun-21-2018, 04:40 AM
Last Post: PrateekG
  Unable to fetch product url using BeautifulSoup with Python3.6 PrateekG 6 4,234 Jun-05-2018, 05:49 PM
Last Post: PrateekG
  Not able to fetch data from a webpage sumandas89 3 4,743 Dec-21-2017, 08:30 AM
Last Post: sumandas89
  HTML select options from python list adi.6194 4 11,662 Oct-20-2016, 01:31 PM
Last Post: adi.6194

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020