0

我有简单的形式女巫有交付选项;取货并张贴。也可以使用折扣券。使用优惠券时,用户必须亲自使用优惠券亲自付款。

我的问题是,当尝试强制执行此行为并在表单clean中使用优惠券时更改单选选项时,它将更改form.cleaned_data但视图将使用原始选择呈现表单。

#forms.py
class DeliveryOptionsForm(forms.Form):
    RADIO_CHOICES = (
       ('pickup',"Pick your stuff from office")),
       ('postit', "send to me by mail (+2€)"),
    )

    delivery = forms.ChoiceField(widget=forms.RadioSelect, 
                           choices=RADIO_CHOICES,
                           help_text=_('Select shipping method'))
    discount = forms.DecimalField(help_text=_('DISCOUNT TICKET'), required=False)

    def clean(self):
        data = self.cleaned_data

        if 'discount' in data:            
            if data['discount'] == None:
                data['discount'] = 0;
            else:
                #force pickup when putting discount coupongs
                data['pickup'] = 'pickup'
        return data

而我的看法

def purchase_confirmation(req):
    cart = req.session['cart']
    form = DeliveryOptionsForm(req.POST or None, initial={'pickup': 'postit'})         

    if req.method == 'POST' and form.is_valid():         
        discount = form.cleaned_data['discount']
        pickup = form.cleaned_data['pickup'] 
        # pickup will be correctly set, for logic, 
        # but form will render selection unchanged
        #Some logic here
        .... 
        if 'confirm' in req.POST:
            return redirect('shop-generating_bill')

        if 'update' in req.POST:   
            pass

    return render(req,"confirmation.html",{'form' : form})
4

1 回答 1

0

不确定这是否是您的问题的原因,但是在您的 clean 方法中,您应该通过调用超类的 clean 方法来获取已清理的数据。

 cleaned_data = super(DeliveryOptionsForm, self).clean()
于 2012-08-10T09:21:18.327 回答