当我运行pprint(request.POST)
时,它只返回表单集最后一个表单的数据。为什么会这样?这是我的代码。
我的models.py
:
class Order(models.Model):
date = models.DateField(unique=True, blank=True, null=True)
first_course = models.CharField(null=True, blank=True, unique=False, max_length=30)
first_course_quantity = models.IntegerField()
second_course = models.CharField(null=True, blank=True, unique=False, max_length=30)
second_course_quantity = models.IntegerField()
dessert = models.CharField(null=True, blank=True, unique=False, max_length=30)
dessert_quantity = models.IntegerField()
drink = models.CharField(null=True, blank=True, unique=False, max_length=30)
drink_quantity = models.IntegerField()
def __str__(self):
return f"Date of order: {self.date}"
我的forms.py
:
class DisabledOptionWidget(forms.Select):
def render(self, name, value, attrs=None, renderer=None):
html_code = super(DisabledOptionWidget, self).render(name, value, attrs, renderer)
html_code = html_code.replace(f'<option value=""', f'<option value="" disabled')
return html_code
class OrderForm(forms.ModelForm):
first_course = forms.ChoiceField(choices=[("", 'Select a dish')] + [(f"{item}", item) for item in list(
Menu.objects.values_list("first_course", flat=True))], widget=DisabledOptionWidget, required=False)
first_course_quantity = forms.IntegerField(min_value=0)
second_course = forms.ChoiceField(choices=[("", 'Select a dish')] + [(f"{item}", item) for item in list(
Menu.objects.values_list("second_course", flat=True))], widget=DisabledOptionWidget, required=False)
second_course_quantity = forms.IntegerField(min_value=0)
dessert = forms.ChoiceField(choices=[("", 'Select a dish')] + [(f"{item}", item) for item in list(
Menu.objects.values_list("dessert", flat=True))], widget=DisabledOptionWidget, required=False)
dessert_quantity = forms.IntegerField(min_value=0)
drink = forms.ChoiceField(choices=[("", 'Select a dish')] + [(f"{item}", item) for item in list(
Menu.objects.values_list("drink", flat=True))], widget=DisabledOptionWidget, required=False)
drink_quantity = forms.IntegerField(min_value=0)
date = forms.DateField(required=False)
class Meta:
model = Order
fields = "__all__"
def save(self, commit=True):
if commit:
order = Order.objects.create(
date=self.cleaned_data['date'],
first_course=self.cleaned_data['first_course'],
first_course_quantity=self.cleaned_data['first_course_quantity'],
second_course=self.cleaned_data['second_course'],
second_course_quantity=self.cleaned_data['second_course_quantity'],
dessert=self.cleaned_data['dessert'],
dessert_quantity=self.cleaned_data['dessert_quantity'],
drink=self.cleaned_data['drink'],
drink_quantity=self.cleaned_data['drink_quantity']
)
return order
我的**views.py:
**
class OrderView(LoginRequiredMixin, CreateView):
login_url = reverse_lazy('login')
template_name = "myapp/order.html"
success_url = reverse_lazy("order_success")
formset_class = formset_factory(OrderForm, extra=5)
def get(self, request, *args, **kwargs):
days = self._get_days()
formset = self.formset_class()
return render(request, self.template_name, {
'formset': formset,
'Monday': days[0],
'Tuesday': days[1],
'Wednesday': days[2],
'Thursday': days[3],
'Friday': days[4]
})
def post(self, request, *args, **kwargs):
formset = self.formset_class(request.POST)
if formset.is_valid():
formset.save()
return redirect(self.success_url)
else:
pprint(request.POST)
return redirect('order')
我的模板看起来像:
{% for form in formset.forms %}
{% if forloop.counter == 1 %}
{% csrf_token %}
<form 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" %}
</form>
{% endif %}
{% if forloop.counter == 2 %}
<form id="second" class="off" method='post'>
{% csrf_token %}
<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" %}
</form>
{% endif %}
{% if forloop.counter == 3 %}
<form id="third" class="off" method='post'>
{% csrf_token %}
<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" %}
</form>
{% endif %}
{% if forloop.counter == 4 %}
<form id="four" class="off" method='post'>
{% csrf_token %}
<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" %}
</form>
{% endif %}
{% if forloop.counter == 5 %}
<form id="five" class="off" method='post'>
{% csrf_token %}
<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" %}
</form>
{% 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>
<script src="{% static 'myapp/js/order.js' %}"></script>
<script>
let date_1 = '{{ Monday }}';
let date_2 = '{{ Tuesday }}';
let date_3 = '{{ Wednesday }}';
let date_4 = '{{ Thursday }}';
let date_5 = '{{ Friday }}';
$('#f1_date').val(date_1);
$('#f2_date').val(date_2);
$('#f3_date').val(date_3);
$('#f4_date').val(date_4);
$('#f5_date').val(date_5);
</script>
2条答案
按热度按时间uqxowvwt1#
您只直接呈现模板中表单集中第一个表单的字段。
formset.forms.0.first_course
这就是为什么你面临这个问题。
您需要放入
for loop
并更改您的render_field
你应该把你的模板改成这样:
这样,它将迭代并创建所有整个表单集
ig9co6j12#
我不得不这么做: