如何将多个表单数据存储到一个数据库模型中

时间:2018-11-12 09:06:26

标签: django-models

我想将多个表单数据存储到一个数据库(模型)中。提交表单后,只需单击一下即可添加两个输入值,这意味着我想单击提交按钮一次即可添加两个类别。

#Form.html文件

    <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文件

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

def __str__(self):
    return self.cname

Views.py文件

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')

0 个答案:

没有答案