如何在字段中插入我的 Django 模型数据以编辑记录
How I can insert my django model data in fields for edit a record
我想编辑我的产品,所有数据都已经在 django 模型中。当我在选项中添加按 "edit" 按钮时,会打开一个新表单来编辑产品,但我不知道在字段中插入数据的对象。请指导我如何在字段 there is the image of my product
中显示数据
and that is edit form if you see before the edit button data is displaying but i want that data in form fields
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'
你必须添加一个实例
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)
我想编辑我的产品,所有数据都已经在 django 模型中。当我在选项中添加按 "edit" 按钮时,会打开一个新表单来编辑产品,但我不知道在字段中插入数据的对象。请指导我如何在字段 there is the image of my product
中显示数据and that is edit form if you see before the edit button data is displaying but i want that data in form fields
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'
你必须添加一个实例
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)