1π
β
If you fetch the user from the db and use that as the form instance, then request.user
should not change.
user = User.objects.get(id=request.user.id)
form_user = UserForm(request.POST or None, instance=user )
As an aside, itβs usually recommended to redirect after a successful post request (even if you redirect to the same url). You can also use the render
shortcut to simplify your code:
from django.shortcuts import render, redirect
def my_view(request):
form_profile = UserProfileForm(request.POST or None, instance = UserProfile.objects.get(user = request.user))
form_user = UserForm(request.POST or None, instance = request.user )
if form_profile.is_valid() and form_user.is_valid():
form_profile.save()
form_user.save()
return redirect('/success-url/')
return render(request, 'users/user_edit.html', {"form_profile":form_profile, "form_user": form_user})
π€Alasdair
Source:stackexchange.com