i want save multiple data database autoincremented id (1,2,3...etc),not in same column. user can dynamically add input fields , click submit button save data in database different id(auto incremented id) each.
i did html , j query add input fields when click button.but don't have idea store in database using django.i did nothing in view.py file store this.
enter image description here code add fields using html , jquery
html:
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script> $(function(){ var =0; $('#adduser").click(function(){ var answerhtml = ""; answerhtml ='<div class="form-group" style="border: 1px solid;background-color: #add8e6">' +' <div class="col-xs-4"><input type="text" name="firstname'+i+'"> </input></div>' +' <div class="col-xs-4"><input type="text" name="age'+i+'"> </input></div>' +' <div class="col-xs-4"><input type="text" name="relation'+i+'"></input></div>' +'<i class="icon-trash" style="padding-left:20px; cursor: pointer;"></i></div>'; $('#divquatationlist').append(answerhtml); i++; $("#totallength").val(i); }); }); $(document).on("click",".icon-trash",function(e){ $(this).closest('.form-group').remove(); }); </script> <body> <div> <p id='adduser' class='btn btn-info' >add</p> </div> <form class="form-horizontal row-border" action="{% url "saveforms" %}" method="post"> <input type="hidden" id="totallength" name="totallength" /> <div id="divquatationlist"></div> <div class="col-md-12"><input type="submit" id="submit" class="btn btn-info pull-right" value="savedata" /> </form> </body> </html> url url(r'^saveforms/$', views.saveforms, name='saveforms'), views def saveforms(request): lenth = request.post['totallength'] if request.post: = 0 index in range(i,int(lenth)): firstname ="" age ="" relation ="" flag=0 if 'firstname'+str(index) in request.post: firstname= request.post['firstname'+str(index)] flag = 1 if 'age'+str(index) in request.post: age= request.post['age'+str(index)] flag = 1 if 'relation'+str(index) in request.post: relation= request.post['relation'+str(index)] flag = 1 if flag == 1: username.objects.create(firstname=firstname,age=age,relation=relation) return httpresponseredirect("/dynamicform/manageforms/")
Comments
Post a Comment