0

I created a form to create a client and another to modify it but the problem is that the modification form does not bring the information of the database.

Bring a basic form without fields with data.

view:

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)
1
  • Where is the code of ClienteForm. Please share your forms.py also. Commented Dec 28, 2016 at 4:46

1 Answer 1

2

You need to pass your Cliente object through instance parameter in your modelform in else block.

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)
Sign up to request clarification or add additional context in comments.

2 Comments

The instance appears to be in the very confusingly named variable queryset, which should certainly be renamed.
I agree, renamed the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.