[Answered ]-Can't get bootstrap's input select dropdown box working with django for loop

2👍

✅

Assuming this div is properly contained within a form tag:

  1. You should be using form.equipment, equipment_id only has the ID numbers.

  2. You’re trying to iterate to create options, but your select tag is inside the for loop, so you’re creating one select for each equipment object, instead of one option for each.

  3. Without the rest of your code, it’s unclear if there are other problems.

I assume what you’re trying to do is get the form to display with bootstrap attributes. A much cleaner solution is to use the package django-bootstrap-forms (pip install django-bootstrap-forms), include it in your template with {% load bootstrap %}, and use as follows:

<form id="form" class="form" method="post">
    {% csrf_token %}
    {{ form|bootstrap }}
    <div class="form-actions">
        <button id="create_submit" class="btn btn-default" type="submit">Save</button>
    </div>
</form>

UPDATE

You’re still iterating over options incorrectly. Correct would be something like:

{% for value,text in form.technician.choices %}
    <option value="{{ value }}">{{ text }}</option>
{% endfor %}

You might run into problems actually posting the form if technician is a foreign key field (only one allowed per model instance). But that’s a separate issue 🙂

Leave a comment