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

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

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 = (
    (&#39;S&#39;, &#39;standard&#39;),
    (&#39;E&#39;, &#39;express&#39;),
)


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 = {
                &#39;object&#39; : order,
                &#39;couponform&#39;: CouponForm(),
                &#39;DISPLAY_COUPON_FORM&#39;: True,
            }
            return render(self.request, &#39;cart.html&#39;, context)
        except ObjectDoesNotExist:
            messages.info(self.request, &quot;You do not have an active order&quot;)
            return redirect(&#39;estores:home&#39;)
    
    def post(self, *args, **kwargs):
        # ...

cart.html

{% for value, name in form.shipping_option.choices %}
&lt;div class=&quot;card&quot;&gt;
    &lt;div class=&quot;card-header&quot; id=&quot;heading-4&quot;&gt;
	    &lt;div class=&quot;card-title&quot;&gt;
            &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;
            &lt;label for=&quot;{{ name }}&quot;&gt;{{ name }}&lt;/label&gt;
        &lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;
{% 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.
form = Shipping()
context = {
    &#39;form&#39;: form,  # Add this
    &#39;object&#39; : order,
    &#39;couponform&#39;: CouponForm(),
    &#39;DISPLAY_COUPON_FORM&#39;: True,
}
  1. form.shipping_option is a BoundField.
    To access the field's choices, do form.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

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:

确定