如何在Django中将表单的多个输入字段数据存储到单个表(模型)中

时间:2018-11-14 06:43:47

标签: django-models django-forms

我想将一种形式的多个输入数据存储到一个数据库(模型)中。提交表单后,只需单击一下即可添加两个输入值,这意味着我想单击提交按钮即可添加两个类别和两个具有唯一ID的描述。

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

1 个答案:

答案 0 :(得分:0)

您可以使用getlist进行操作,并将cname和desc压缩为一个元组,然后将zip转换为list并添加到数据库中。但是要这样做,您需要给html输入元素的name属性必须相同。

HTML表单文件

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

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