[Answer]-Django form not saving

1👍

Have you tried to print form._errors when the form is invalid? It’s a good way of debugging issues like this. Here you have fixed code:

def update_user(request):
    user = Customer.objects.get(pk=request.user.id)
    form = CustomerForm(instance=user)
    if request.method == 'POST':
        form = CustomerForm(request.POST, instance=user) # HERE
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('success.html')

    args = {}
    args.update(csrf(request))
    args['form'] = form
    return render_to_response('update.html', args)

Additionally you could replace this code:

form = CustomerForm(instance=user)
if request.method == 'POST':
    form = CustomerForm(request.POST, instance=user) # HERE
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('success.html')

with this one (works the same):

form = CustomerForm(request.POST or None, instance=user)
if form.is_valid():
    form.save()
    return HttpResponseRedirect('success.html')

Leave a comment