I have a 'Product'
Model in which all the details of a product is available and I have a 'Order'
Model with Foreign Key of Product
Model. I want a dropdown list in From.html
with all product name of Product model and if I select any one of them then all the details should automatically filled in the input field. I applied this concept on a particular field like this:
Product.objects.filter(pname='Lenovo K8 Plus').values('pname','catgry','brand')
From.Html
<form class="well form-horizontal" method="post" action="{% url 'new_order' %}">
{% csrf_token %}
<fieldset>
<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">
{% for n in ListPrdt %}
<option>{{n.pname}}</option>
{% endfor %}
</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>
{% for n in ListPrdt %}
<input id="fullName" name="catgry" placeholder="Full Name" class="form-control" required="true" value="{{n.catgry}}" type="text">
{% endfor %}
</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>
{% for n in ListPrdt %}
<input id="fullName" name="brand" placeholder="Full Name" class="form-control" required="true" value="{{n.brand}}" type="text">
{% endfor %}
</div>
</div>
</div>
<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>
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
Views.py File
def NewOrder(request):
add_prdt = Product.objects.filter(pname='Lenovo K8 Plus').values('pname','catgry','brand')
return render(request, 'neworder.html', {'ListPrdt' : add_prdt})
Product Model
class Product(models.Model):
pname = models.CharField(max_length=50)
catgry = models.CharField(max_length=20)
brand = models.CharField(max_length=20)
price = models.CharField(max_length=10)
qnty = models.CharField(max_length=10)
vname = models.CharField(max_length=50)
mfd = models.CharField(max_length=20)
exp = models.CharField(max_length=10)
height = models.CharField(max_length=10)
width = models.CharField(max_length=10)
color = models.CharField(max_length=10)
wght = models.CharField(max_length=10)
remark = models.CharField(max_length=50)
hsn = models.CharField(max_length=10)
def __str__(self):
return self.pname
Views.py File(For add new order in Order Model)
def new_order(request):
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')
Comments
Post a Comment