Bringing Select-Option values to Select-Option

Viewed 29

I made a drop-down field that I created with select-option, after saving the data I selected here, when I enter the update form, I want the data selected in the save form to be selected again, can you help with this?

insert.html

<select name="healmedy_car_info" id="healmedy_car_info" class="form-select" aria-label="Default select example">
                                    <option value="selectcar">Lütfen Araç Seçiniz</option>
                                    <option value="34SAS20">34 SAS 20</option>
                                    <option value="34SAS30">34 SAS 30</option>
                                    <option value="34BF2904">34 BF 2904</option>
                                    <option value="34TP0633">34 TP 0633</option>
                                    <option value="34BF9529">34 BF 9529</option>
                                </select>

update.html

<select name="healmedy_car_info" id="healmedy_car_info" class="form-select" aria-label="Default select example">
                                    <option value="selectcar">Lütfen Araç Seçiniz</option>
                                    <option value="34SAS20">34 SAS 20</option>
                                    <option value="34SAS30">34 SAS 30</option>
                                    <option value="34BF2904">34 BF 2904</option>
                                    <option value="34TP0633">34 TP 0633</option>
                                    <option value="34BF9529">34 BF 9529</option>
                                </select>

I pull the data with django and save it to the database, I just want the data I pulled here to be selected in update.html.

1 Answers

Does your Django View return what specific one is selected? i.e. which value is selected? When you said in the UpdateForm, I assume you have that value from the DB or something.

If so you can just add a conditional to each option like this:

<select name="healmedy_car_info" id="healmedy_car_info" class="form-select" aria-label="Default select example">
   <option value="selectcar" {% if form.healmedy_car_info == 'selectcar' %}selected{% endif %}>Lütfen Araç Seçiniz</option>
   <option value="34SAS20" {% if form.healmedy_car_info == '34SAS20' %}selected{% endif %}>34 SAS 20</option>
   <option value="34SAS30" {% if form.healmedy_car_info == '34SAS30' %}selected{% endif %}>34 SAS 30</option>
   <option value="34BF2904" {% if form.healmedy_car_info == '34BF2904' %}selected{% endif %}>34 BF 2904</option>
   <option value="34TP0633" {% if form.healmedy_car_info == '34TP0633' %}selected{% endif %}>34 TP 0633</option>
   <option value="34BF9529" {% if form.healmedy_car_info == '34BF9529' %}selected{% endif %}>34 BF 9529</option>
</select>

Better yet, to avoid repetition, if you had a list of the "Allowed" values available in the template, you could do something like:

<select name="healmedy_car_info" id="healmedy_car_info" class="form-select" aria-label="Default select example">
  {% for option in options %}
   <option value="{{ option.value }}" {% if form.healmedy_car_info == option.value %}selected{% endif %}>{{option.label}}</option>
  {% endfor %}
</select>
Related