[Django]-How to populate select option dropdown with database info in django

8πŸ‘

βœ…

In view.py get all the options that you want to see in your HTML

options = yourmodels.objects.filter(foo=bar)

Then pass it in your context dictionary

context = {'options': options, ...}

Then In Your HTML

<select id="playerselect" name="pitcher" class="player-dropdown">
{% for option in options %}
    <option value="{{ option.id}}">{{ option.player_name }}</option>
{% endfor %}
<input type="submit" value="Search"/>
</select>

Leave a comment