2

i have one form and use it many times with a for loop in one template file. I want to set the values of the fields in my templates directly. I use Django-bootstrap4 and didn't now how to realize it.

I tried the template tags "bootstrap_form" and "bootstrap_field", but I can't set the value of the fields.

1) for loop with the form without initializing the values (worked):

{% for item in items %}
    <div class="modal fade" id="item_modal_{{item.id}}" ...>
        <div class="modal-content">
            <div class="modal-body">
                {% bootstrap_form form %}
            </div>
        </div>
    </div>
{% endfor %}

2) Now I want to initial the form fields with the item values. I tried this:

{% for item in items %}
    <div class="modal fade" id="item_modal_{{item.id}}" ...>
        <div class="modal-content">
            <div class="modal-body">
                {% bootstrap_field form.field1 value=item.field1 %}
                {% bootstrap_field form.field2 value=item.field2 %}
                {% bootstrap_field form.field3 value=item.field3 %}
                {% bootstrap_field form.field4 value=item.field4 %}
            </div>
        </div>
    </div>
{% endfor %}

Thank you!

  • Possible duplicate of [Django set default form values](https://stackoverflow.com/questions/604266/django-set-default-form-values) – Davit Tovmasyan Oct 10 '19 at 10:16
  • Thank you for your answer @DavitTovmasyan. I can't initial the data directly in my forms, because I have one form for many different values (for each item in the for loop). So I think I must initial it in the template. :-/ – Alessandro Napolitano Oct 10 '19 at 10:25

0 Answers0