I want to edit my product which all data is already in django model. When i add press the "edit" button in option a new form is open to edit a product but I don't know who to insert data in fields. Kindly give me guidance how i display me data in fields there is the image of my product
views.py
class EditProduct(TemplateView):
template_name = 'stock/editproduct.html'
def get(self, request, product_id):
productedit = get_object_or_404(Product, pk=product_id)
form = EditProductForm()
args = {'form':form, 'productedit':productedit}
return render(request, self.template_name, args)
template.html
{% extends 'base.html' %}
{% block content %}
<div>
<h4>Edit Product!</h4>
<hr/>
<form method="post" enctype="multipart/form-data" >
{% csrf_token %}
{{ form.as_p }}
<h4>{{productedit.pro_name}}</h4>
<p>{{productedit.companyName}}</p>
<p>{{productedit.Sale_Price}}</p>
<p>{{productedit.Quantity}}</p>
<button type="submit" class="btn btn-success" >Edit</button>
</form>
</div>
{% endblock %}
form.py
class EditProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ('companyName', 'pro_name', 'Purchase_Price', 'Sale_Price', 'Quantity', 'Picture' )
def __init__(self, *args, **kwargs):
super(EditProductForm, self).__init__(*args, **kwargs)
self.fields['companyName'].label = 'Company Name'
self.fields['pro_name'].label = 'Product Name'
self.fields['Purchase_Price'].label = 'Purchase Price'
self.fields['Sale_Price'].label = 'Sale Price'
you have to add an instance
views.py
def get(self, request, product_id):
productedit = get_object_or_404(Product, pk=product_id)
data=Product.objects.filter(id=product_id)
form = EditProductForm(instance=data)
args = {'form':form, 'productedit':productedit}
return render(request, self.template_name, args)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.