barmirbar
barmirbar

Reputation: 133

How can I set form field value in a template in django forms?

So, here I have a piece of HTML code that is supposed to render a form using django form class. I have several forms on one page, they are rendered in 'for' cycle and should have different values as default (appointment.id). How can I set a value of a field here inside a template? Is in at least possible?

{% for appointment in appointments%}
{% load crispy_forms_tags %}
<form action="/register/" method="post" class="inline">
    {% csrf_token %}
    <div class="form-group w-25">
        <div class="col-sm-10">
            {{form.appointment_id|as_crispy_field}}
        </div>
        <input type="submit" class="btn btn-primary mt-1" value="Register">
    </div>
</form>
</p>
{% endfor %}

Upvotes: 3

Views: 943

Answers (1)

Kyrylo Kundik
Kyrylo Kundik

Reputation: 64

You can do it in that way:

{% for appointment in appointments%}
{% load crispy_forms_tags %}
<form action="/register/" method="post" class="inline">
    {% csrf_token %}
    <div class="form-group w-25">
        <div class="col-sm-10">
            {{ form.appointment_id(value=appointment.id)|as_crispy_field }}
        </div>
        <input type="submit" class="btn btn-primary mt-1" value="Register">
    </div>
</form>
</p>
{% endfor %}

Upvotes: 1

Related Questions