How to store multiple input field data of a form into single table( model) in django










0















I want to store multiple input data of a single form into one database(Model). After submitting the form the two input value will be added in one click means I want to add two category and two description with unique id of both in one click of submit button.



Form.html File



 <form class="well form-horizontal" method="post" action="% url 'add_category' %">
% csrf_token %
<fieldset>
<div class="form-group">
<label class="col-md-4 control-label">Category Name</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="fullName" name="cname" placeholder="Full Name" class="form-control" required="true" value="" type="text"></div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Description</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
<input id="addressLine1" name="desc" placeholder="Address Line 1" class="form-control" required="true" value="" type="text"></div>
</div>
</div>


<div class="form-group">
<label class="col-md-4 control-label">Category Name</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="fullName" name="cname1" placeholder="Full Name" class="form-control" required="true" value="" type="text"></div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Description</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
<input id="addressLine1" name="desc1" placeholder="Address Line 1" class="form-control" required="true" value="" type="text"></div>
</div>
</div>
<button>Submit</button>
</fieldset>
</form>


Model.Py File



class Category(models.Model): 
cname = models.CharField(max_length=20)
desc = models.CharField(max_length=50)

def __str__(self):
return self.cname


Views.Py File



def add_category(request):
print("from is submitted successfully!")
cname = request.POST.get(["cname","cname1"], False)
desc = request.POST.get(["desc","desc1"], False)

CategoryAdd = Category(cname = [cname,cname1], desc = [desc,desc1])
CategoryAdd.save()

return render(request,'addcategory.html')









share|improve this question


























    0















    I want to store multiple input data of a single form into one database(Model). After submitting the form the two input value will be added in one click means I want to add two category and two description with unique id of both in one click of submit button.



    Form.html File



     <form class="well form-horizontal" method="post" action="% url 'add_category' %">
    % csrf_token %
    <fieldset>
    <div class="form-group">
    <label class="col-md-4 control-label">Category Name</label>
    <div class="col-md-6 inputGroupContainer">
    <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
    <input id="fullName" name="cname" placeholder="Full Name" class="form-control" required="true" value="" type="text"></div>
    </div>
    </div>
    <div class="form-group">
    <label class="col-md-4 control-label">Description</label>
    <div class="col-md-6 inputGroupContainer">
    <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
    <input id="addressLine1" name="desc" placeholder="Address Line 1" class="form-control" required="true" value="" type="text"></div>
    </div>
    </div>


    <div class="form-group">
    <label class="col-md-4 control-label">Category Name</label>
    <div class="col-md-6 inputGroupContainer">
    <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
    <input id="fullName" name="cname1" placeholder="Full Name" class="form-control" required="true" value="" type="text"></div>
    </div>
    </div>
    <div class="form-group">
    <label class="col-md-4 control-label">Description</label>
    <div class="col-md-6 inputGroupContainer">
    <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
    <input id="addressLine1" name="desc1" placeholder="Address Line 1" class="form-control" required="true" value="" type="text"></div>
    </div>
    </div>
    <button>Submit</button>
    </fieldset>
    </form>


    Model.Py File



    class Category(models.Model): 
    cname = models.CharField(max_length=20)
    desc = models.CharField(max_length=50)

    def __str__(self):
    return self.cname


    Views.Py File



    def add_category(request):
    print("from is submitted successfully!")
    cname = request.POST.get(["cname","cname1"], False)
    desc = request.POST.get(["desc","desc1"], False)

    CategoryAdd = Category(cname = [cname,cname1], desc = [desc,desc1])
    CategoryAdd.save()

    return render(request,'addcategory.html')









    share|improve this question
























      0












      0








      0








      I want to store multiple input data of a single form into one database(Model). After submitting the form the two input value will be added in one click means I want to add two category and two description with unique id of both in one click of submit button.



      Form.html File



       <form class="well form-horizontal" method="post" action="% url 'add_category' %">
      % csrf_token %
      <fieldset>
      <div class="form-group">
      <label class="col-md-4 control-label">Category Name</label>
      <div class="col-md-6 inputGroupContainer">
      <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
      <input id="fullName" name="cname" placeholder="Full Name" class="form-control" required="true" value="" type="text"></div>
      </div>
      </div>
      <div class="form-group">
      <label class="col-md-4 control-label">Description</label>
      <div class="col-md-6 inputGroupContainer">
      <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
      <input id="addressLine1" name="desc" placeholder="Address Line 1" class="form-control" required="true" value="" type="text"></div>
      </div>
      </div>


      <div class="form-group">
      <label class="col-md-4 control-label">Category Name</label>
      <div class="col-md-6 inputGroupContainer">
      <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
      <input id="fullName" name="cname1" placeholder="Full Name" class="form-control" required="true" value="" type="text"></div>
      </div>
      </div>
      <div class="form-group">
      <label class="col-md-4 control-label">Description</label>
      <div class="col-md-6 inputGroupContainer">
      <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
      <input id="addressLine1" name="desc1" placeholder="Address Line 1" class="form-control" required="true" value="" type="text"></div>
      </div>
      </div>
      <button>Submit</button>
      </fieldset>
      </form>


      Model.Py File



      class Category(models.Model): 
      cname = models.CharField(max_length=20)
      desc = models.CharField(max_length=50)

      def __str__(self):
      return self.cname


      Views.Py File



      def add_category(request):
      print("from is submitted successfully!")
      cname = request.POST.get(["cname","cname1"], False)
      desc = request.POST.get(["desc","desc1"], False)

      CategoryAdd = Category(cname = [cname,cname1], desc = [desc,desc1])
      CategoryAdd.save()

      return render(request,'addcategory.html')









      share|improve this question














      I want to store multiple input data of a single form into one database(Model). After submitting the form the two input value will be added in one click means I want to add two category and two description with unique id of both in one click of submit button.



      Form.html File



       <form class="well form-horizontal" method="post" action="% url 'add_category' %">
      % csrf_token %
      <fieldset>
      <div class="form-group">
      <label class="col-md-4 control-label">Category Name</label>
      <div class="col-md-6 inputGroupContainer">
      <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
      <input id="fullName" name="cname" placeholder="Full Name" class="form-control" required="true" value="" type="text"></div>
      </div>
      </div>
      <div class="form-group">
      <label class="col-md-4 control-label">Description</label>
      <div class="col-md-6 inputGroupContainer">
      <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
      <input id="addressLine1" name="desc" placeholder="Address Line 1" class="form-control" required="true" value="" type="text"></div>
      </div>
      </div>


      <div class="form-group">
      <label class="col-md-4 control-label">Category Name</label>
      <div class="col-md-6 inputGroupContainer">
      <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
      <input id="fullName" name="cname1" placeholder="Full Name" class="form-control" required="true" value="" type="text"></div>
      </div>
      </div>
      <div class="form-group">
      <label class="col-md-4 control-label">Description</label>
      <div class="col-md-6 inputGroupContainer">
      <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
      <input id="addressLine1" name="desc1" placeholder="Address Line 1" class="form-control" required="true" value="" type="text"></div>
      </div>
      </div>
      <button>Submit</button>
      </fieldset>
      </form>


      Model.Py File



      class Category(models.Model): 
      cname = models.CharField(max_length=20)
      desc = models.CharField(max_length=50)

      def __str__(self):
      return self.cname


      Views.Py File



      def add_category(request):
      print("from is submitted successfully!")
      cname = request.POST.get(["cname","cname1"], False)
      desc = request.POST.get(["desc","desc1"], False)

      CategoryAdd = Category(cname = [cname,cname1], desc = [desc,desc1])
      CategoryAdd.save()

      return render(request,'addcategory.html')






      django-models django-forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 6:43









      Abhishek KumarAbhishek Kumar

      54




      54






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You can use getlist for the operation and zip the cname and desc to get a tuple and convert zip to list and add to database. But to do so you need to give your html input elements name attribute must be same.



          HTML Form File



          <form>
          <input name="cname" placeholder="Full name"/>
          <input name="desc" placeholder="Description"/>
          <input name="cname" placeholder="Full name"/>
          <input name="desc" placeholder="Description"/>
          </form>


          Model.Py File



          class Category(models.Model): 
          cname = models.CharField(max_length=20)
          desc = models.CharField(max_length=50)

          def __str__(self):
          return self.cname


          Views.Py File



          def add_category(request):
          print("from is submitted successfully!")
          cname = request.POST.getlist("cname")
          desc = request.POST.getlist("desc")
          if len(cname)==len(desc) and cname and desc:
          mapped=zip(cname,desc)
          mapped=list(mapped)
          for ele in mapped:
          CategoryAdd,created = Category.objects.get_or_create(cname = ele[0],desc=ele[1])
          return render(request,'addcategory.html')





          share|improve this answer

























          • ok wait. I'm doing.

            – Abhishek Kumar
            Nov 15 '18 at 8:03











          • It's Working but the problem is both category and desc is added in one row.

            – Abhishek Kumar
            Nov 15 '18 at 8:27











          • I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

            – Abhishek Kumar
            Nov 15 '18 at 8:28











          • ill update the answer. that would be simple. i added above answer based on your code.

            – Pavan Kumar T S
            Nov 15 '18 at 9:50











          • I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

            – Abhishek Kumar
            Nov 15 '18 at 10:51










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294479%2fhow-to-store-multiple-input-field-data-of-a-form-into-single-table-model-in-dj%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can use getlist for the operation and zip the cname and desc to get a tuple and convert zip to list and add to database. But to do so you need to give your html input elements name attribute must be same.



          HTML Form File



          <form>
          <input name="cname" placeholder="Full name"/>
          <input name="desc" placeholder="Description"/>
          <input name="cname" placeholder="Full name"/>
          <input name="desc" placeholder="Description"/>
          </form>


          Model.Py File



          class Category(models.Model): 
          cname = models.CharField(max_length=20)
          desc = models.CharField(max_length=50)

          def __str__(self):
          return self.cname


          Views.Py File



          def add_category(request):
          print("from is submitted successfully!")
          cname = request.POST.getlist("cname")
          desc = request.POST.getlist("desc")
          if len(cname)==len(desc) and cname and desc:
          mapped=zip(cname,desc)
          mapped=list(mapped)
          for ele in mapped:
          CategoryAdd,created = Category.objects.get_or_create(cname = ele[0],desc=ele[1])
          return render(request,'addcategory.html')





          share|improve this answer

























          • ok wait. I'm doing.

            – Abhishek Kumar
            Nov 15 '18 at 8:03











          • It's Working but the problem is both category and desc is added in one row.

            – Abhishek Kumar
            Nov 15 '18 at 8:27











          • I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

            – Abhishek Kumar
            Nov 15 '18 at 8:28











          • ill update the answer. that would be simple. i added above answer based on your code.

            – Pavan Kumar T S
            Nov 15 '18 at 9:50











          • I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

            – Abhishek Kumar
            Nov 15 '18 at 10:51















          0














          You can use getlist for the operation and zip the cname and desc to get a tuple and convert zip to list and add to database. But to do so you need to give your html input elements name attribute must be same.



          HTML Form File



          <form>
          <input name="cname" placeholder="Full name"/>
          <input name="desc" placeholder="Description"/>
          <input name="cname" placeholder="Full name"/>
          <input name="desc" placeholder="Description"/>
          </form>


          Model.Py File



          class Category(models.Model): 
          cname = models.CharField(max_length=20)
          desc = models.CharField(max_length=50)

          def __str__(self):
          return self.cname


          Views.Py File



          def add_category(request):
          print("from is submitted successfully!")
          cname = request.POST.getlist("cname")
          desc = request.POST.getlist("desc")
          if len(cname)==len(desc) and cname and desc:
          mapped=zip(cname,desc)
          mapped=list(mapped)
          for ele in mapped:
          CategoryAdd,created = Category.objects.get_or_create(cname = ele[0],desc=ele[1])
          return render(request,'addcategory.html')





          share|improve this answer

























          • ok wait. I'm doing.

            – Abhishek Kumar
            Nov 15 '18 at 8:03











          • It's Working but the problem is both category and desc is added in one row.

            – Abhishek Kumar
            Nov 15 '18 at 8:27











          • I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

            – Abhishek Kumar
            Nov 15 '18 at 8:28











          • ill update the answer. that would be simple. i added above answer based on your code.

            – Pavan Kumar T S
            Nov 15 '18 at 9:50











          • I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

            – Abhishek Kumar
            Nov 15 '18 at 10:51













          0












          0








          0







          You can use getlist for the operation and zip the cname and desc to get a tuple and convert zip to list and add to database. But to do so you need to give your html input elements name attribute must be same.



          HTML Form File



          <form>
          <input name="cname" placeholder="Full name"/>
          <input name="desc" placeholder="Description"/>
          <input name="cname" placeholder="Full name"/>
          <input name="desc" placeholder="Description"/>
          </form>


          Model.Py File



          class Category(models.Model): 
          cname = models.CharField(max_length=20)
          desc = models.CharField(max_length=50)

          def __str__(self):
          return self.cname


          Views.Py File



          def add_category(request):
          print("from is submitted successfully!")
          cname = request.POST.getlist("cname")
          desc = request.POST.getlist("desc")
          if len(cname)==len(desc) and cname and desc:
          mapped=zip(cname,desc)
          mapped=list(mapped)
          for ele in mapped:
          CategoryAdd,created = Category.objects.get_or_create(cname = ele[0],desc=ele[1])
          return render(request,'addcategory.html')





          share|improve this answer















          You can use getlist for the operation and zip the cname and desc to get a tuple and convert zip to list and add to database. But to do so you need to give your html input elements name attribute must be same.



          HTML Form File



          <form>
          <input name="cname" placeholder="Full name"/>
          <input name="desc" placeholder="Description"/>
          <input name="cname" placeholder="Full name"/>
          <input name="desc" placeholder="Description"/>
          </form>


          Model.Py File



          class Category(models.Model): 
          cname = models.CharField(max_length=20)
          desc = models.CharField(max_length=50)

          def __str__(self):
          return self.cname


          Views.Py File



          def add_category(request):
          print("from is submitted successfully!")
          cname = request.POST.getlist("cname")
          desc = request.POST.getlist("desc")
          if len(cname)==len(desc) and cname and desc:
          mapped=zip(cname,desc)
          mapped=list(mapped)
          for ele in mapped:
          CategoryAdd,created = Category.objects.get_or_create(cname = ele[0],desc=ele[1])
          return render(request,'addcategory.html')






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 10:04

























          answered Nov 14 '18 at 13:57









          Pavan Kumar T SPavan Kumar T S

          540417




          540417












          • ok wait. I'm doing.

            – Abhishek Kumar
            Nov 15 '18 at 8:03











          • It's Working but the problem is both category and desc is added in one row.

            – Abhishek Kumar
            Nov 15 '18 at 8:27











          • I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

            – Abhishek Kumar
            Nov 15 '18 at 8:28











          • ill update the answer. that would be simple. i added above answer based on your code.

            – Pavan Kumar T S
            Nov 15 '18 at 9:50











          • I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

            – Abhishek Kumar
            Nov 15 '18 at 10:51

















          • ok wait. I'm doing.

            – Abhishek Kumar
            Nov 15 '18 at 8:03











          • It's Working but the problem is both category and desc is added in one row.

            – Abhishek Kumar
            Nov 15 '18 at 8:27











          • I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

            – Abhishek Kumar
            Nov 15 '18 at 8:28











          • ill update the answer. that would be simple. i added above answer based on your code.

            – Pavan Kumar T S
            Nov 15 '18 at 9:50











          • I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

            – Abhishek Kumar
            Nov 15 '18 at 10:51
















          ok wait. I'm doing.

          – Abhishek Kumar
          Nov 15 '18 at 8:03





          ok wait. I'm doing.

          – Abhishek Kumar
          Nov 15 '18 at 8:03













          It's Working but the problem is both category and desc is added in one row.

          – Abhishek Kumar
          Nov 15 '18 at 8:27





          It's Working but the problem is both category and desc is added in one row.

          – Abhishek Kumar
          Nov 15 '18 at 8:27













          I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

          – Abhishek Kumar
          Nov 15 '18 at 8:28





          I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

          – Abhishek Kumar
          Nov 15 '18 at 8:28













          ill update the answer. that would be simple. i added above answer based on your code.

          – Pavan Kumar T S
          Nov 15 '18 at 9:50





          ill update the answer. that would be simple. i added above answer based on your code.

          – Pavan Kumar T S
          Nov 15 '18 at 9:50













          I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

          – Abhishek Kumar
          Nov 15 '18 at 10:51





          I want that both the categories and desc should be added in model with different id's. @ Pavan Kumar TS

          – Abhishek Kumar
          Nov 15 '18 at 10:51



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294479%2fhow-to-store-multiple-input-field-data-of-a-form-into-single-table-model-in-dj%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto