B
B
bielikovv2022-04-07 13:48:58
Django
bielikovv, 2022-04-07 13:48:58

How to edit model data through a form?

There is an extended User model, there is a form that is associated with this model. How to implement a function for editing data?

def show_user_form(request):
    if request.method == 'POST':
        form = RedactInfoUserForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            redirect('profile')
    else:
        form = RedactInfoUserForm()
    return render(request, 'shop/user_profile.html', {'form':form})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-04-07
@bielikovv

Add a parameter to the form (in both calls):

form = RedactInfoUserForm(instance=some_user) # some_user - КОНКРЕТНЫЙ юзер из бд, выбранный по id/логину и т.д.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question