如何从Django模型获取特定文件的所有详细信息并使用Ajax填充为表单

时间:2018-12-20 12:04:58

标签: django-models django-forms

我有两个模型,一个是产品模型,另一个是订单模型。订单模型具有产品名称,该名称与产品模型具有外键关系。 有一张订购单,我希望下拉列表中的所有产品名称都进入“选择产品名称的形式”部分,当我选择其中任何一种时,它们的类别名称和品牌名称应填充到表格中。实现此目标的ajax查询将是什么。通过orm是否还有其他选择。

Form.html

<!DOCTYPE html>
<html>
<head>
  <title>Add Brand List</title>

</head>
<body>
<h3>Purchased Order - Add New Order</h3>
<div class="container">
       <table class="table table-striped">
          <tbody>
             <tr>
                <td colspan="1">
                   <form class="well form-horizontal" method="post" action="{% url 'new_order' %}">
                    {% csrf_token %}
                      <fieldset>
                           {% for n in ListPrdt %}
                         <div class="form-group">
                            <label class="col-md-4 control-label">Select Product</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group">
                                  <span class="input-group-addon" style="max-width: 100%;"><i class="glyphicon glyphicon-list"></i></span>
                                  <select class="selectpicker form-control" name="pname">
                                     <option>{{n.pname}}</option>   
                                  </select>
                               </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="catgry" placeholder="Full Name" class="form-control" required="true" value="{{n.catgry}}" type="text">

                              </div>
                            </div>
                         </div>

                         <div class="form-group">
                            <label class="col-md-4 control-label">Brand 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="brand" placeholder="Full Name" class="form-control" required="true" value="{{n.brand}}" type="text">
                              </div>
                            </div>
                         </div>
                       {% endfor %}

                           <div class="form-group">
                            <label class="col-md-4 control-label">Quantity</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span><input id="email" name="qnty" placeholder="Email" class="form-control" required="true" value="" type="text"></div>
                            </div>
                         </div>
                         <div class="form-group">
                            <label class="col-md-4 control-label">Price</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span><input id="phoneNumber" name="price" placeholder="Phone Number" class="form-control" required="true" value="" type="text"></div>
                            </div>
                         </div>

                         <div class="form-group">
                            <label class="col-md-4 control-label">Tax</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="addressLine2" name="tax" placeholder="Address Line 2" class="form-control" required="true" value="" type="text"></div>
                            </div>
                         </div>
                         <div class="form-group">
                            <label class="col-md-4 control-label">Total</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="city" name="total" placeholder="City" class="form-control" required="true" value="" type="text"></div>
                            </div>
                         </div>
                         <div class="form-group">
                            <label class="col-md-4 control-label">Delivery Date</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="state" name="deliverydt" placeholder="State/Province/Region" class="form-control" required="true" value="" type="text"></div>
                            </div>
                         </div>
                         <div class="form-group">
                            <label class="col-md-4 control-label">Remarks</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="postcode" name="remark" placeholder="Postal Code/ZIP" class="form-control" required="true" value="" type="text"></div>
                            </div>
                         </div>
                         <div class="form-group">
                          <label class="col-md-4 control-label">Select Brand</label>
                            <div class="col-md-6 inputGroupContainer">
                               <div class="input-group">
                                  <span class="input-group-addon" style="max-width: 100%;"><i class="glyphicon glyphicon-list"></i></span>
                                  <select class="selectpicker form-control" name="conrej" disabled> 
                                     <option>Confirm</option>
                                     <option>Rejected</option>
                                  </select>
                               </div>
                            </div>
                         </div>
                        <button>Submit</button>

                      </fieldset>
                   </form>
                </td>                    
             </tr>
          </tbody>
       </table>
    </div>
  </body>

Models.py(产品型号)

class Product(models.Model):


pname = models.CharField(max_length=50, default='prdtname')
    catgry = models.CharField(max_length=20, default='catgry')
    brand = models.CharField(max_length=20, default='brand')
    price = models.CharField(max_length=10, default='200')
    qnty = models.CharField(max_length=10, default='2')
    vname = models.CharField(max_length=50, default='abcd')
    mfd = models.CharField(max_length=20, default='12-05-2018')
    exp = models.CharField(max_length=10, default='12-05-2019')
    height = models.CharField(max_length=10, default='2m')
    width = models.CharField(max_length=10, default='5cm')
    color = models.CharField(max_length=10, default='blue')
    wght = models.CharField(max_length=10, default='200g')
    remark = models.CharField(max_length=50, default='good')
    hsn = models.CharField(max_length=10, default='13234')

    def __str__(self):
        return self.pname

Models.py(订购商品)

class Order(models.Model):
    Reject = 'RJ'
    Confirm = 'CN'
    confchoice = ((Reject, 'Reject'),(Confirm, 'Confirm'),)
    pname = models.ForeignKey(Product, on_delete=models.CASCADE)
    catgry = models.CharField(max_length=10)
    brand = models.CharField(max_length=50)
    qnty = models.CharField(max_length=10)
    price = models.CharField(max_length=20)
    tax = models.CharField(max_length=20)
    total = models.CharField(max_length=20)
    deliverydt = models.CharField(max_length=20)
    remark = models.CharField(max_length=10)
    conrej = models.CharField(max_length=10, choices=confchoice, default="Reject")

    def __str__(self):
        return self.remark

View.py

def new_order(request):
    print("Saving loan is submitted successfully!")
    pname = request.POST.get("pname", False)
    catgry = request.POST.get("catgry", False)
    brand = request.POST.get("brand", False)
    qnty = request.POST.get("qnty", False)
    price = request.POST.get("price", False)
    tax = request.POST.get("tax", False)
    total = request.POST.get("total", False)
    deliverydt = request.POST.get("deliverydt", False)
    remark = request.POST.get("remark", False)
    OrderNew = Order(pname = pname, catgry = catgry, brand = brand, qnty = qnty, price = price, tax = tax, 
                        total = total, deliverydt = deliverydt, remark = remark)
    OrderNew.save()

    return render(request,'neworder.html')

0 个答案:

没有答案