私は簡単なフォームを持っています。魔女には配達のオプションがあります。ピックアップして投稿します。割引クーポンを使用する可能性もあります。クーポンを使用する場合、ユーザーは常にクーポンを持って直接行って支払う必要があります。
私の問題は、フォームクリーンでクーポンを使用するときにこの動作を強制してラジオの選択を変更しようとすると、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})