close

How to create dropdown menu from python list using Flask and HTML

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to create dropdown menu from python list using Flask and HTML in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to create dropdown menu from python list using Flask and HTML?

  1. How to create dropdown menu from python list using Flask and HTML?

    ou need to use {{colour}} in both places (instead of {{colours}} in the second place):
    <select name="colour" method="GET" action="/">

  2. create dropdown menu from python list using Flask and HTML

    ou need to use {{colour}} in both places (instead of {{colours}} in the second place):
    <select name="colour" method="GET" action="/">

Method 1

You need to use {{colour}} in both places (instead of {{colours}} in the second place):

<select name="colour" method="GET" action="/">
    {% for colour in colours %}
        <option value="{{colour}}" SELECTED>{{colour}}</option>"
    {% endfor %}
</select>

Note that using selected inside the loop will add selected attribute to all options and the last one will be selected, what you need to do is the following:

<select name="colour" method="GET" action="/">
  <option value="{{colours[0]}}" selected>{{colours[0]}}</option>
  {% for colour in colours[1:] %}
    <option value="{{colour}}">{{colour}}</option>
  {% endfor %}
</select>

Method 2

you have a typo, replace colours to colour

<option value= "{{colour}}" SELECTED>{{colours}}</option>"

replace to

<option value= "{{colour}}" SELECTED>{{ colour }}</option>"
                                     <!--  ^^^^ -->

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read