1👍
✅
Your are directly rendering the fields for only the first form in the formset in your template. formset.forms.0.first_course
That is why you are facing this issue.
You need to put in a for loop
and change your render_field
You should change your template to something like this:
<form id="first" class="off on" method='post'>
{% csrf_token %}
{% for form in formset.forms %}
<div class="main">
<ul>
<li><h2 class="frst">First Course</h2></li>
<li><h2 class="qnt1">Quantity</h2></li>
<li><h2 class="fprc">Price</h2></li>
</ul>
<ul>
{% render_field form.first_course id='f1_fc' %}
{% render_field form.first_course_quantity id='f1_fcq' class="sq" type="number" %}
<div class="sq" id='f1_fcp'></div>
</ul><br>
... Other form fields
</div>
{% endfor %}
</form> ...other code...
{% render_field formset.forms.0.date id='f1_date' style="display:none" %}
<script>
let date_1 = '{{ Monday }}';
$('#f1_date').val(date_1);
</script>
This way it will iterate and create all the entire formset
0👍
I had to do something like that:
<form method='POST' id='main_form'>
{% csrf_token %}
{% for form in formset.forms %}
{% if forloop.counter == 1 %}
<div id="first" class="off on" method='post'>
<div class="main">
<ul>
<li><h2 class="frst">First Course</h2></li>
<li><h2 class="qnt1">Quantity</h2></li>
<li><h2 class="fprc">Price</h2></li>
</ul>
<ul>
{% render_field form.first_course id='f1_fc' %}
{% render_field form.first_course_quantity id='f1_fcq' class="sq" type="number" %}
<div class="sq" id='f1_fcp'></div>
</ul><br>
<ul>
<li><h2 class="tst">Second Course</h2></li>
<li><h2 class="qnt2">Quantity</h2></li>
<li><h2 class="prc">Price</h2></li>
</ul>
<ul>
{% render_field form.second_course id='f1_sc' %}
{% render_field form.second_course_quantity id='f1_scq' class="sq" type="number" %}
<div class="sq" id='f1_scp'></div>
</ul><br>
<ul>
<li><h2>Dessert</h2></li>
<li><h2 class="qnt3">Quantity</h2></li>
<li><h2>Price</h2></li>
</ul>
<ul>
{% render_field form.dessert id='f1_des' %}
{% render_field form.dessert_quantity id='f1_des_q' class="sq" type="number" %}
<div class="sq" id='f1_des_p' ></div>
</ul><br>
<ul>
<li><h2>Drinks</h2></li>
<li><h2 class="qnt4">Quantity</h2></li>
<li><h2>Price</h2></li>
</ul>
<ul>
{% render_field form.drink id='f1_dr' %}
{% render_field form.drink_quantity id='f1_dr_q' class="sq" type="number" %}
<div class="sq" id='f1_dr_p' ></div>
</ul>
</div>
{% render_field form.date id='f1_date' style="display:none" %}
</div>
{% endif %}
{% if forloop.counter == 2 %}
<div id="second" class="off" method='post'>
<div class="main">
<ul>
<li><h2 class="frst">First Course</h2></li>
<li><h2 class="qnt1">Quantity</h2></li>
<li><h2 class="fprc">Price</h2></li>
</ul>
<ul>
{% render_field form.first_course id='f2_fc' %}
{% render_field form.first_course_quantity id='f2_fcq' class="sq" type="number" %}
<div class="sq" id='f2_fcp'></div>
</ul><br>
<ul>
<li><h2 class="tst">Second Course</h2></li>
<li><h2 class="qnt2">Quantity</h2></li>
<li><h2 class="prc">Price</h2></li>
</ul>
<ul>
{% render_field form.second_course id='f2_sc' %}
{% render_field form.second_course_quantity id='f2_scq' class="sq" type="number" %}
<div class="sq" id='f2_scp' ></div>
</ul><br>
<ul>
<li><h2>Dessert</h2></li>
<li><h2 class="qnt3">Quantity</h2></li>
<li><h2>Price</h2></li>
</ul>
<ul>
{% render_field form.dessert id='f2_des' %}
{% render_field form.dessert_quantity id='f2_des_q' class="sq" type="number" %}
<div class="sq" id='f2_des_p'></div>
</ul><br>
<ul>
<li><h2>Drinks</h2></li>
<li><h2 class="qnt4">Quantity</h2></li>
<li><h2>Price</h2></li>
</ul>
<ul>
{% render_field form.drink id='f2_dr' %}
{% render_field form.drink_quantity id='f2_dr_q' class="sq" type="number" %}
<div class="sq" id='f2_dr_p'></div>
</ul>
</div>
{% render_field form.date id='f2_date' style="display:none" %}
</div>
{% endif %}
{% if forloop.counter == 3 %}
<div id="third" class="off" method='post'>
<div class="main">
<ul>
<li><h2 class="frst">First Course</h2></li>
<li><h2 class="qnt1">Quantity</h2></li>
<li><h2 class="fprc">Price</h2></li>
</ul>
<ul>
{% render_field form.first_course id='f3_fc' %}
{% render_field form.first_course_quantity id='f3_fcq' class="sq" type="number" %}
<div class="sq" id='f3_fcp'></div>
</ul><br>
<ul>
<li><h2 class="tst">Second Course</h2></li>
<li><h2 class="qnt2">Quantity</h2></li>
<li><h2 class="prc">Price</h2></li>
</ul>
<ul>
{% render_field form.second_course id='f3_sc' %}
{% render_field form.second_course_quantity id='f3_scq' class="sq" type="number" %}
<div class="sq" id='f3_scp'></div>
</ul><br>
<ul>
<li><h2>Dessert</h2></li>
<li><h2 class="qnt3">Quantity</h2></li>
<li><h2>Price</h2></li>
</ul>
<ul>
{% render_field form.dessert id='f3_des' %}
{% render_field form.dessert_quantity id='f3_des_q' class="sq" type="number" %}
<div class="sq" id='f3_des_p' ></div>
</ul><br>
<ul>
<li><h2>Drinks</h2></li>
<li><h2 class="qnt4">Quantity</h2></li>
<li><h2>Price</h2></li>
</ul>
<ul>
{% render_field form.drink id='f3_dr' %}
{% render_field form.drink_quantity id='f3_dr_q' class="sq" type="number" %}
<div class="sq" id='f3_dr_p' ></div>
</ul>
</div>
{% render_field form.date id='f3_date' style="display:none" %}
</div>
{% endif %}
{% if forloop.counter == 4 %}
<div id="four" class="off" method='post'>
<div class="main">
<ul>
<li><h2 class="frst">First Course</h2></li>
<li><h2 class="qnt1">Quantity</h2></li>
<li><h2 class="fprc">Price</h2></li>
</ul>
<ul>
{% render_field form.first_course id="f4_fc" %}
{% render_field form.first_course_quantity id="f4_fcq" class="sq" type="number" %}
<div class="sq" id="f4_fcp"></div>
</ul><br>
<ul>
<li><h2 class="tst">Second Course</h2></li>
<li><h2 class="qnt2">Quantity</h2></li>
<li><h2 class="prc">Price</h2></li>
</ul>
<ul>
{% render_field form.second_course id='f4_sc' %}
{% render_field form.second_course_quantity id='f4_scq' class="sq" type="number" %}
<div class="sq" id='f4_scp'></div>
</ul><br>
<ul>
<li><h2>Dessert</h2></li>
<li><h2 class="qnt3">Quantity</h2></li>
<li><h2>Price</h2></li>
</ul>
<ul>
{% render_field form.dessert id='f4_des' %}
{% render_field form.dessert_quantity id='f4_des_q' class="sq" type="number" %}
<div class="sq" id='f4_des_p'></div>
</ul><br>
<ul>
<li><h2>Drinks</h2></li>
<li><h2 class="qnt4">Quantity</h2></li>
<li><h2>Price</h2></li>
</ul>
<ul>
{% render_field form.drink id='f4_dr' %}
{% render_field form.drink_quantity id='f4_dr_q' class="sq" type="number" %}
<div class="sq" id='f4_dr_p' ></div>
</ul>
</div>
{% render_field form.date id='f4_date' style="display:none" %}
</div>
{% endif %}
{% if forloop.counter == 5 %}
<div id="five" class="off" method='post'>
<div class="main">
<ul>
<li><h2 class="frst">First Course</h2></li>
<li><h2 class="qnt1">Quantity</h2></li>
<li><h2 class="fprc">Price</h2></li>
</ul>
<ul>
{% render_field form.first_course id='f5_fc' %}
{% render_field form.first_course_quantity id='f5_fcq' class="sq" type="number" %}
<div class="sq" id='f5_fcp' ></div>
</ul><br>
<ul>
<li><h2 class="tst">Second Course</h2></li>
<li><h2 class="qnt2">Quantity</h2></li>
<li><h2 class="prc">Price</h2></li>
</ul>
<ul>
{% render_field form.second_course id='f5_sc' %}
{% render_field form.second_course_quantity id='f5_scq' class="sq" type="number" %}
<div class="sq" id='f5_scp'></div>
</ul><br>
<ul>
<li><h2>Dessert</h2></li>
<li><h2 class="qnt3">Quantity</h2></li>
<li><h2>Price</h2></li>
</ul>
<ul>
{% render_field form.dessert id='f5_des' %}
{% render_field form.dessert_quantity id='f5_des_q' class="sq" type="number" %}
<div class="sq" id='f5_des_p'></div>
</ul><br>
<ul>
<li><h2>Drinks</h2></li>
<li><h2 class="qnt4">Quantity</h2></li>
<li><h2>Price</h2></li>
</ul>
<ul>
{% render_field form.drink id='f5_dr' %}
{% render_field form.drink_quantity id='f5_dr_q' class="sq" type="number" %}
<div class="sq" id='f5_dr_p'></div>
</ul>
</div>
{% render_field form.date id='f5_date' style="display:none" %}
</div>
{% endif %}
{% endfor %}
<div class="buttons">
<button class="b1" onclick="backBtn()" id="back" style="visibility: hidden;">Back</button>
<button class="b1" onclick="nextBtn()" id="next"><a href='#'>Next</a></button>
</div>
</form>
- [Answered ]-Update / Save model date value directly from template?
- [Answered ]-Django Faking Required Fields
- [Answered ]-Where to add a method for a model?
- [Answered ]-Django form fails to render_to_response
Source:stackexchange.com