Reputation: 13
I'm trying to handle three forms in one view. I explain : It's a checkout form which have a form for shipping adress, billing adress, credit card info.
My problem it's that I want to let the choice to the user to choose if the billing adress is the same of the shipping adress. I made this with a checkbox.
But now I'm stuck at the validation because of the three forms. I don't know how to handle them.
Here is my code :
def show_checkout(request, template_name='checkout/checkout.html'):
user = User.objects.filter(pk=request.user.id)
if request.method == 'POST':
form = CheckoutForm(request.POST)
form_adress = ShippingForm(request.POST)
if form.is_valid() and form_adress.is_valid():
response = process(request)
form_adress.save(commit=False)
if not form_adress.same_adress:
form_adress.user_shipping = user
form_adress.user_billing = user
else:
form_adress.user_shipping = user
form_billing = BillingForm(request.POST)
form_billing.save(commit=False)
form_billing.user_billing = user
form_billing.save()
form_adress.save()
order_number = response.get('order_number', 0)
error_message = response.get('message', '')
if order_number:
request.session['order_number'] = order_number
receipt_url = urlresolvers.reverse('checkout_receipt')
return HttpResponseRedirect(receipt_url)
else:
error_message = 'Correct the errors below'
else:
form = CheckoutForm()
form_adress = ShippingForm()
form_billing = BillingForm()
error_message = None
if cart.is_empty(request):
cart_url = urlresolvers.reverse('show_cart')
return HttpResponseRedirect(cart_url)
page_title = 'Checkout'
return render(request, template_name, {
'error_message': error_message,
'page_title': page_title,
'form': form,
'form_adress': form_adress,
'form_billing': form_billing,
},
context_instance=RequestContext(request))
And my template :
<form action="." method="post" class="row-fluid">
{% csrf_token %}
<div class="adress span10">
<fieldset class="checkout" style="float:left;">
<legend>Shipping Info</legend>
{{ form_adress.as_p }}
</fieldset>
<fieldset class="checkout billing_adress" style="float:left;">
<legend>Billing Info</legend>
{{ form_billing.as_p }}
</fieldset>
</div>
<fieldset class="checkout row-fluid">
<legend>Credit Card Info</legend>
<table>
{% form_table_row form.name %}
{% form_table_row form.lastname %}
{% form_table_row form.credit_card_number %}
{% form_table_row form.credit_card_type %}
{% form_table_row form.credit_card_expire_month %}
{% form_table_row form.credit_card_expire_year %}
{% form_table_row form.credit_card_cvv %}
</table>
</fieldset>
<table>
<tr>
<th colspan="2"><input type="submit" value="Place Order" class="submit" /></th>
</tr>
</table>
</form>
and my forms :
class ShippingForm(forms.ModelForm):
same_adress = forms.BooleanField(required=True, label="different address for billing ?")
class Meta:
model = Address
exclude = 'user_billing, user_shipping'
class BillingForm(forms.ModelForm):
class Meta:
model = Address
exclude = 'user_billing, user_shipping'
For the moment I'm facing the local variable 'form_billing' referenced before assignment
Any suggestions ?
Thank you in advance !
Upvotes: 0
Views: 168
Reputation: 53316
You are not initializing form_billing
when form_adress
is valid and form_adress.same_adress
is false and order_number
is 0.
You are referencing it in context to render()
.
Also, looks like this if
if not form_adress.same_adress:
should be
if form_adress.same_adress:
Upvotes: 1