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
#1
Hello Coders,

I'm still working on my project for the course, and I'm not giving up, even if everything looks to slow me down!

I'm at the point where I'm building the page edit_recipe.html to edit an existing recipe. Honestly I could do everything, I'm just wasting a lot of time on the select tag. I want to take the value category.name from MongoDB, to use an if to see if they match, and in that case <option> takes the value selected, otherwise not. How can achieve this? Please let me know if you want me to explain more, I hope the code (that is completely wrong) it already explains my purpose well. Thank you!!!

        <select id="category" name="category_name">
          <option value="pasta">Pasta</option>
          <option value="meatfish" {% if recipe.category_name == 'meatfish': "selected" %}>Meat&Fish</option>{%endif%}
          <option value="vegetarian">Vegetarian</option>
          <option value="dessert">Dessert</option>
          <option value="baking">Baking</option>
        </select>
Reply
#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
#3
<option value="meatfish" {{ 'selected' if recipe.category_name == 'meatfish' else '' }} >Meat&Fish</option>
You could use the expression syntax to print "selected" or "".
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
(Jul-12-2020, 05:40 PM)DeaD_EyE Wrote:
<option value="meatfish" {{ 'selected' if recipe.category_name == 'meatfish' else '' }} >Meat&Fish</option>
You could use the expression syntax to print "selected" or "".

Thank you! Your code is a very clean and simple solution :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a Python class for Option deberi 5 2,756 Apr-28-2020, 08:05 AM
Last Post: buran
  #python .... Need to develop a if condition from two lists ginu 2 2,768 Oct-05-2018, 12:45 PM
Last Post: gruntfutuk
  Questions about the GET and POST submission form Functions in Python for a Trivia Gam martin28 8 3,988 Aug-05-2018, 09:47 PM
Last Post: snippsat
  click menu option 6 it does not print out what I need it to AMRiley23 4 3,860 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