任何人都可以帮助我,我在django中制作一个CRUD,我想设置Edit部分的表单字段,其中包含我正在选择的项目的实际数据。谁能告诉我怎么能这样做呢。
forms.py
class editProductForm(forms.ModelForm):
class Meta:
model = Product
exclude = ('object_id', 'slugp', 'category', 'add_date')
views.py
def product_update(request, object_id):
product = Product.objects.get(pk=object_id)
category = product.category
info_send = False
if request.method == "POST":
form = editProductForm(request.POST, request.FILES)
if form.is_valid():
name = form.cleaned_data['name']
description = form.cleaned_data['description']
price = form.cleaned_data['price']
stock = form.cleaned_data['stock']
image = form.cleaned_data['image']
manufacturer = form.cleaned_data['manufacturer']
product.name = name
product.description = description
product.price = price
product.stock = stock
product.image = image
product.manufacturer = manufacturer
product.save()
info = "Updated"
info_send = True
else:
info = "ERROR updating"
return HttpResponseRedirect('/catalog/'+category+'/product/')
else:
form = editProductForm()
ctx = {'form':form}
return render_to_response('dashboard/edit_product.html', ctx, context_instance=RequestContext(request))
edit_product.html
{% extends "base.html" %}
{% load i18n %}
{% load bootstrap %}
{% block title %}Product {{product.name}}{% endblock %}
{% block content %}
<div class="container">
<div class="row text-center">
<div class="alert alert-info">
<h4>Here you can edit items</h4>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-container">
<form id="edit_product_form" action="." method="POST" class="form" enctype="multipart/form-data" >
{% csrf_token %}
{{ form|bootstrap }}
<input id="btnSaveProduct" class="btn btn-primary" type="submit" value="Save">
<input class="btn btn-warning" type="reset" value="Clean">
</form>
</div>
</div>
</div>
</div>
{% endblock %}
谢谢如果有人可以告诉我如何设置表单字段,那么所选项目的实际数据请帮帮我!
答案 0 :(得分:2)
您需要instance
option in the ModelForm
一些事情:
get_object_or_404
instance=
传递给初始化,并POST
:editProductForm(instance=product)
和editProductForm(request.POST, request.FILES, instance=product)
return
语句的缩进。[]
代替.get()
。 试试这个:
def product_update(request, object_id):
product = get_object_or_404(Product, pk=object_id)
category = product.category
info_send = False
if request.method == "POST":
form = editProductForm(request.POST, request.FILES, instance=product)
if form.is_valid():
cd = form.cleaned_data
name = cd.get('name') #user get() instead of []
description = form.cleaned_data['description']
price = form.cleaned_data['price']
stock = form.cleaned_data['stock']
image = form.cleaned_data['image']
manufacturer = form.cleaned_data['manufacturer']
product.name = cd.get('name') #user get() instead of []
product.description = description
product.price = price
product.stock = stock
product.image = image
product.manufacturer = manufacturer
product.save()
info = "Updated"
info_send = True
return HttpResponseRedirect('/catalog/'+category+'/product/')
else:
print form.errors
info = "ERROR updating"
else:
form = editProductForm(instance=product)
ctx = {'form':form}
return render_to_response('dashboard/edit_product.html', ctx, context_instance=RequestContext(request))
答案 1 :(得分:1)
如果我理解您的问题,您可能希望在模板中显示:
<table>
{% for field in form %}
<tr>
<td>
{{ field.label_tag }}
</td>
<td>
{{ field.value }}
</td>
</tr>
{% endfor %}
</table>
这将为您的所有字段显示该字段的当前值。