我创建了一个表单来创建一个客户端而另一个表单来修改它,但问题是修改表单没有带来数据库的信息。
带一个没有数据字段的基本表单。
视图:
def cliente_update(request, id=None):
queryset = get_object_or_404(Cliente, id=id)
if request.method == 'POST':
form = ClienteForm(request.POST or None, instance=instance)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
# return HttpResponseRedirect(instance.get_absolute_url())
else:
form = ClienteForm()
context = {
"titulo": "Editar informacion del Cliente",
"queryset": queryset,
"form": form,
}
return render(request, "clientes/form.html", context)
答案 0 :(得分:2)
您需要通过instance
块中的模型中的else
参数传递Cliente对象。
def cliente_update(request, id=None):
instance = get_object_or_404(Cliente, id=id) # renamed from queryset to instance
if request.method == 'POST':
form = ClienteForm(request.POST or None, instance=instance)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
# return HttpResponseRedirect(instance.get_absolute_url())
else:
form = ClienteForm(instance=instance)
context = {
"titulo": "Editar informacion del Cliente",
"form": form,
}
return render(request, "clientes/form.html", context)