How to save two conditional data from Django Form?

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
                    <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"
                       <---- 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>
                    <<=======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"
                        <label for="shipping-toggle">Ship to a different address?</label>
                    <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"
                            <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"
                        <div class="form-group">
                            <label>Company name (optional)</label>
                            <input type="text" class="form-control form-control-md" id ="companyname" name="company-name">
                        <--- Other Shipping Infor goes here --->
```<<=======form-group checkbox-toggle ends here =====>
Order notes (optional)
                        <div class="form-group place-order pt-6">
                            <button type="submit" class="btn btn-dark btn-block btn-rounded">Place Order</button>

I am saving Billing info from Model form this way:

class CheckoutForm(forms.ModelForm):

class Meta:
model = Order

fields = ['firstname',
          'street_address_1','street_address_2','town','zip','phone','email'] :  

def checkout(request):
if request.method==“POST”:
form = CheckoutForm(request.POST)
if form.is_valid():

    order =Order(firstname=firstname,lastname=lastname,
    return render(request, './ecommerce/order.html')

Well the first thing here is that you’re duplicating a lot of work.

When you’re working with a submitted form, you don’t need to retrieve all the individual fields or to manually create the object - that’s the function of a model form.

As for the rest, it would be helpful if you posted the Models and the other forms involved.

Its the model :
class Order(models.Model):

    items =models.CharField(max_length=1000)

    product = models.ForeignKey(Products,on_delete=models.DO_NOTHING,blank=True, null=True)

    cutomer = models.ForeignKey(Customer, on_delete=models.CASCADE,null=True, related_name='product_order')

    firstname =models.CharField(max_length=1000, blank=True, null=True)

    lastname =models.CharField(max_length=1000, blank=True, null=True)

    companyname =models.CharField(max_length=1000, blank=True, null=True)

    email = models.CharField(max_length=1000, blank=True, null=True)

    country =  models.CharField(max_length=1000, blank=True, null=True)

    address = models.CharField(max_length=1000, blank=True, null=True)

    town = models.CharField(max_length=1000, blank=True, null=True)

    state= models.CharField(max_length=1000, blank=True, null=True)

    street_address_1 =models.CharField(max_length=1000, blank=True, null=True)

    street_address_2 =models.CharField(max_length=1000, blank=True, null=True)


    zip =models.CharField(max_length=1000)

    phone =models.CharField(max_length=1000)    

    order_status = models.CharField(choices=ORDER_STATUS,max_length=50, default= "pending review")

    total = models.CharField(max_length=200)

and there is no other form involved at the moment, but I want to save data of billing and shipping address from same form as shown above, I am stuck with this idea.

First, please remember to enclose blocks of code between lines of three backtick characters.

Ok, but the point is, if you have a Model Form with all the desired fields defined in it, the process of saving the data in the form consists only of something to the effect of:

my_form = ModelFormDefintion(request.POST)
if my_form.is_valid():

See Creating forms from models | Django documentation | Django

That’s it. Notice what’s not there - I’m not getting all the individual form fields, nor am I manually creating an instance of the model being saved.

All that code you have in your view is doing the same work twice.