I have a form where the shipping address part will be visible when custom-checkbox input is checked. I am saving data to model when the checkbox is not checked.
I want to save the shipping form and billing form if the toggle class is checked and if not billing info only. How can I do this? Any help will be highly appreciated.
How should I deal with form to save shipping and billing address when checkbox is checked on following form:
<form class="form checkout-form" method="POST" action="">
<div class="row mb-9">
<div class="col-lg-7 pr-lg-4 mb-4">
<h3 class="title billing-title text-uppercase ls-10 pt-1 pb-3 mb-0">
Billing Details
</h3>
<div class="row gutter-sm">
<div class="col-xs-6">
<div class="form-group">
<label>First name *</label>
<input type="text" class="form-control form-control-md" id="firstname" name="firstname"
required>
</div>
</div>
<---- Other Required Billing Info Goes Here---->
<div class="form-group mb-7">
<label>Email address *</label>
<input type="email" class="form-control form-control-md" id="email" name="email" required>
</div>
<<=======Shipping form toggle section starts here =====>
<div class="form-group checkbox-toggle pb-2">
<input type="checkbox" class="custom-checkbox" id="shipping-address" id="shipping-toggle"
name="shipping-toggle">
<label for="shipping-toggle">Ship to a different address?</label>
</div>
<div class="checkbox-content">
<div class="row gutter-sm">
<div class="col-xs-6">
<div class="form-group">
<label>First name *</label>
<input type="text" class="form-control form-control-md"id ="firstname" name="firstname"
required>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label>Last name *</label>
<input type="text" class="form-control form-control-md" id="lastname" name="lastname"
required>
</div>
</div>
</div>
<div class="form-group">
<label>Company name (optional)</label>
<input type="text" class="form-control form-control-md" id ="companyname" name="company-name">
</div>
<--- Other Shipping Infor goes here --->
```<<=======form-group checkbox-toggle ends here =====>
<div class="form-group place-order pt-6">
<button type="submit" class="btn btn-dark btn-block btn-rounded">Place Order</button>
</div>
</div>
</div>
</div>
</div>
</form>
I am saving Billing info from Model form this way:
class CheckoutForm(forms.ModelForm):
class Meta:
model = Order
fields = ['firstname',
'lastname','companyname',
'street_address_1','street_address_2','town','zip','phone','email']
Views.py :
def checkout(request):
if request.method==“POST”:
form = CheckoutForm(request.POST)
if form.is_valid():
firstname=request.POST.get(‘name’,"")
lastname=request.POST.get(‘name’,"")
companyname=request.POST.get(‘companyname’,"")
street_address_1=request.POST.get('street_address_1',"")
town=request.POST.get('town',"")
zip=request.POST.get('zip',"")
phone=request.POST.get('phone',"")
email=request.POST.get('email',"")
order =Order(firstname=firstname,lastname=lastname,
companyname=companyname,street_address_1=street_address_1,
email=email,town=town,zip=zip,phone=phone,
)
order.save()
return render(request, './ecommerce/order.html')