英文:
Form field choices not showing when loop through in Django template
问题
forms.py
SHIPPING_CHOICES = (
('S', '标准'),
('E', '快递'),
)
class Shipping(forms.Form):
shipping_option = forms.ChoiceField(widget=forms.RadioSelect, choices=SHIPPING_CHOICES)
cost = forms.CharField(required=False)
views.py
class OrderSummaryView(LoginRequiredMixin, View):
def get(self, *args, **kwargs):
try:
order = Order.objects.get(user=self.request.user, ordered=False)
form = Shipping()
context = {
'object': order,
'couponform': CouponForm(),
'DISPLAY_COUPON_FORM': True,
}
return render(self.request, 'cart.html', context)
except ObjectDoesNotExist:
messages.info(self.request, "您没有活动订单")
return redirect('estores:home')
def post(self, *args, **kwargs):
# ...
cart.html
{% for value, name in form.shipping_option.choices %}
<div class="card">
<div class="card-header" id="heading-4">
<div class="card-title">
<input id="{{ name }}" type="radio" name="shipping_option" value="{{ value }}" class="button" required>
<label for="{{ name }}">{{ name }}</label>
</div>
</div>
</div>
{% endfor %}
Note: I have translated the comments in the code as well for clarity.
英文:
forms.py
SHIPPING_CHOICES = (
('S', 'standard'),
('E', 'express'),
)
class Shipping(forms.Form):
shipping_option = forms.ChoiceField(widget=forms.RadioSelect, choices=SHIPPING_CHOICES)
cost = forms.CharField(required=False)
views.py
class OrderSummaryView(LoginRequiredMixin, View):
def get(self, *args, **kwargs):
try:
order = Order.objects.get(user=self.request.user, ordered=False)
form = Shipping()
context = {
'object' : order,
'couponform': CouponForm(),
'DISPLAY_COUPON_FORM': True,
}
return render(self.request, 'cart.html', context)
except ObjectDoesNotExist:
messages.info(self.request, "You do not have an active order")
return redirect('estores:home')
def post(self, *args, **kwargs):
# ...
cart.html
{% for value, name in form.shipping_option.choices %}
<div class="card">
<div class="card-header" id="heading-4">
<div class="card-title">
<input id="{{ name }}" type="radio" name="shipping_option" value="{{ value }}" class="button" required>
<label for="{{ name }}">{{ name }}</label>
</div>
</div>
</div>
{% endfor %}
I don't know why but I have been trying to make it work but it won't work at all. I want to be able to loop through the choices available for shipping options.
So far I was able to do something like that for the payment option and it worked.
答案1
得分: 0
-
在
context
中传递'form': form
。 -
form.shipping_option
是一个BoundField
。
要访问字段的choices
,请使用form.shipping_option.field.choices
。
英文:
- Pass
'form': form
incontext
.
form = Shipping()
context = {
'form': form, # Add this
'object' : order,
'couponform': CouponForm(),
'DISPLAY_COUPON_FORM': True,
}
form.shipping_option
is aBoundField
.
To access the field'schoices
, doform.shipping_option.field.choices
.
{% for value, name in form.shipping_option.choices %} # Change this
{% for value, name in form.shipping_option.field.choices %} # to this
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论