Python Forum

Full Version: How do I fetch values from db to Select Options using Flask?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
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>