Python Forum
form select option tags, add "selected" on <option> in python at a condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form select option tags, add "selected" on <option> in python at a condition
#2
I could make it in the following way, but I know that this is the longest solution, for sure there is a better way, but as a student it's ok. If anybody has a better idea, please share it thank you.

        <select id="category" name="category_name">

          {% if recipe.category_name == "pasta" %}
            <option value="pasta" selected>Pasta</option>
          {% else %} 
            <option value="pasta">Pasta</option>
          {% endif %}

          {% if recipe.category_name == "meatfish" %}
            <option value="meatfish" selected>Meat&Fish</option>
          {% else %} 
            <option value="meatfish">Meat&Fish</option>
          {% endif %}

          {% if recipe.category_name == "vegetarian" %}
            <option value="vegetarian" selected>Vegetarian</option>
          {% else %}
            <option value="vegetarian">Vegetarian</option>
          {% endif %}

          {% if recipe.category_name == "dessert" %}
            <option value="dessert" selected>Dessert</option>
          {% else %}
            <option value="dessert">Dessert</option>
          {% endif %}

          {% if recipe.category_name == "baking" %}
            <option value="baking" selected>Baking</option>
          {% else %}
            <option value="baking">Baking</option>
          {% endif %}

        </select>
Reply


Messages In This Thread
RE: form select option tags, add "selected" on <option> in python at a condition - by Leon79 - Jul-12-2020, 02:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  a Python class for Option deberi 5 2,912 Apr-28-2020, 08:05 AM
Last Post: buran
  #python .... Need to develop a if condition from two lists ginu 2 2,908 Oct-05-2018, 12:45 PM
Last Post: gruntfutuk
  click menu option 6 it does not print out what I need it to AMRiley23 4 3,994 Nov-27-2017, 09:03 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020