Home > Software design >  Flask-Sqlite: not visualizing username list from database in the dropdown menu
Flask-Sqlite: not visualizing username list from database in the dropdown menu

Time:12-02

I am new to web dev with Python Flask and SQlalchemy and I am trying to populate a a dropdown menu with usernames from the table "user" for a Kanban board. I was able to fetch the datas from the database but for some reason they are not displayed in the dropdown menu in my dashboard template. Actually the dropdown menu is populated but the names are not displayed:

enter image description here

app.py

from flask import g
import sqlite3

def get_db():
    DATABASE = 'C:/path/to/db'
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db

@app.route('/dashboard')
def dashboard():
      cur = get_db().cursor()
      team_members = cur.execute("SELECT username FROM user").fetchall()
      print(team_members)
      return render_template('dashboard.html', team_members=team_members)

dashboard.html

<div >
<p style="font-family:verdana">
    Choose a team member:<SELECT name="team_members_usernames" style="font-family:verdana">
    {% for t in team_members %}
        <OPTION value={{t[0]}}>{{t[1]}}</OPTION>
    {% endfor %}
    </SELECT>
    </p>
</div> 

In the console I am getting the usernames like this:

[('User_1',), ('User_2',), ('User_3',), ('User_4',)]

What am I doing wrong? I would be thankful for every tip!

CodePudding user response:

The usernames you're getting in a console is a list of tuples. Each tuple has only one item which you're accessing in dashboard.html like this: <OPTION value={{t[0]}}>{{t[1]}}</OPTION> The second item {{t[1}} is None therefore you are getting no visible text in a select field. Try to go like this: <OPTION value={{t[0]}}>{{t[0]}}</OPTION>

  • Related