0

I would like to use the user's data as a value in SelectField. I am trying to create a list of things the user has added to the database, then the user can see what they have added. So it should loop through the database and list the items the user has chosen. The user can then see what they have listed and be able to edit the SelectField at their choosing.

# Ingredients Form
ITEM_AMOUNT = [(0, "-"), (1, "1/4"), (2, "1/2"), (3, "1/3"), (4, "1/8"), (5, "1"), (6, "2"), 
(7, "3"), (8, "4"), (9, "5"), (10, "6")]

class ListForm(FlaskForm):
    item = SelectField("Amount", choices=ITEM_AMOUNT)

# CSS
{% extends 'base.html' %}
{% block content %}

<form action="/dashboard" method="Post">
{{ form.hidden_tag() }}
{% for choice in list %}
    {{ form.item(class="form-select", placeholder=dict(form.item.choices).get(form.item.data)) 
}}
{% endfor %}
{% endblock %}
  • Does this answer your question? [How do I make a placeholder for a 'select' box?](https://stackoverflow.com/questions/5805059/how-do-i-make-a-placeholder-for-a-select-box) – pjcunningham May 11 '22 at 09:23

1 Answers1

0

You can use the flask form's init function to define default values for your flask forms from your database in the following way:

class ListForm(FlaskForm):
    item = SelectField("Amount", choices=ITEM_AMOUNT)
    def __init__(self, *args, **kwargs):
       super(ListForm, self).__init__(*args, **kwargs)
       self.item.choices = *Query from database* #Value from query should be a list 
                                              of choices
Harris Minhas
  • 702
  • 3
  • 17