表单字段选项在Django模板中循环时未显示。

huangapple go评论92阅读模式
英文:

Form field choices not showing when loop through in Django template

问题

forms.py

  1. SHIPPING_CHOICES = (
  2. ('S', '标准'),
  3. ('E', '快递'),
  4. )
  5. class Shipping(forms.Form):
  6. shipping_option = forms.ChoiceField(widget=forms.RadioSelect, choices=SHIPPING_CHOICES)
  7. cost = forms.CharField(required=False)

views.py

  1. class OrderSummaryView(LoginRequiredMixin, View):
  2. def get(self, *args, **kwargs):
  3. try:
  4. order = Order.objects.get(user=self.request.user, ordered=False)
  5. form = Shipping()
  6. context = {
  7. 'object': order,
  8. 'couponform': CouponForm(),
  9. 'DISPLAY_COUPON_FORM': True,
  10. }
  11. return render(self.request, 'cart.html', context)
  12. except ObjectDoesNotExist:
  13. messages.info(self.request, "您没有活动订单")
  14. return redirect('estores:home')
  15. def post(self, *args, **kwargs):
  16. # ...

cart.html

  1. {% for value, name in form.shipping_option.choices %}
  2. <div class="card">
  3. <div class="card-header" id="heading-4">
  4. <div class="card-title">
  5. <input id="{{ name }}" type="radio" name="shipping_option" value="{{ value }}" class="button" required>
  6. <label for="{{ name }}">{{ name }}</label>
  7. </div>
  8. </div>
  9. </div>
  10. {% endfor %}

Note: I have translated the comments in the code as well for clarity.

英文:

forms.py

  1. SHIPPING_CHOICES = (
  2. (&#39;S&#39;, &#39;standard&#39;),
  3. (&#39;E&#39;, &#39;express&#39;),
  4. )
  5. class Shipping(forms.Form):
  6. shipping_option = forms.ChoiceField(widget=forms.RadioSelect, choices=SHIPPING_CHOICES)
  7. cost = forms.CharField(required=False)

views.py

  1. class OrderSummaryView(LoginRequiredMixin, View):
  2. def get(self, *args, **kwargs):
  3. try:
  4. order = Order.objects.get(user=self.request.user, ordered=False)
  5. form = Shipping()
  6. context = {
  7. &#39;object&#39; : order,
  8. &#39;couponform&#39;: CouponForm(),
  9. &#39;DISPLAY_COUPON_FORM&#39;: True,
  10. }
  11. return render(self.request, &#39;cart.html&#39;, context)
  12. except ObjectDoesNotExist:
  13. messages.info(self.request, &quot;You do not have an active order&quot;)
  14. return redirect(&#39;estores:home&#39;)
  15. def post(self, *args, **kwargs):
  16. # ...

cart.html

  1. {% for value, name in form.shipping_option.choices %}
  2. &lt;div class=&quot;card&quot;&gt;
  3. &lt;div class=&quot;card-header&quot; id=&quot;heading-4&quot;&gt;
  4. &lt;div class=&quot;card-title&quot;&gt;
  5. &lt;input id=&quot;{{ name }}&quot; type=&quot;radio&quot; name=&quot;shipping_option&quot; value=&quot;{{ value }}&quot; class=&quot;button&quot; required&gt;
  6. &lt;label for=&quot;{{ name }}&quot;&gt;{{ name }}&lt;/label&gt;
  7. &lt;/div&gt;
  8. &lt;/div&gt;
  9. &lt;/div&gt;
  10. {% 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

  1. context 中传递 &#39;form&#39;: form

  2. form.shipping_option 是一个 BoundField
    要访问字段的 choices,请使用 form.shipping_option.field.choices

英文:
  1. Pass &#39;form&#39;: form in context.
  1. form = Shipping()
  2. context = {
  3. &#39;form&#39;: form, # Add this
  4. &#39;object&#39; : order,
  5. &#39;couponform&#39;: CouponForm(),
  6. &#39;DISPLAY_COUPON_FORM&#39;: True,
  7. }
  1. form.shipping_option is a BoundField.
    To access the field's choices, do form.shipping_option.field.choices.
  1. {% for value, name in form.shipping_option.choices %} # Change this
  2. {% for value, name in form.shipping_option.field.choices %} # to this

huangapple
  • 本文由 发表于 2023年5月14日 07:09:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定