Python Forum

Full Version: form select option tags, add "selected" on <option> in python at a condition
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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>
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>
<option value="meatfish" {{ 'selected' if recipe.category_name == 'meatfish' else '' }} >Meat&Fish</option>
You could use the expression syntax to print "selected" or "".
(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 :)