0

while editing my profile page ,i get blank page but i want previous saved data.

forms.py

class UserEditForm(forms.ModelForm):
    class Meta:
        model=User
        fields=['first_name','last_name','email']

class ProfileEditForm(forms.ModelForm):
    class Meta:
        model=Profile
        fields=['date_of_birth','photo']

views.py

@login_required
def editprofile(request):
    user_form=UserEditForm()
    profile_form=ProfileEditForm()
    if request.method=='POST':
       user_form=UserEditForm(data=request.POST,instance=request.user)
       profile_form=ProfileEditForm(data=request.POST,instance=request.user.profile,files=request.FILES)
       if user_form.is_valid() and profile_form.is_valid() :
         user_form.save()
         profile_form.save()
         return HttpResponse('profile saved')
    context={'user_form':user_form,'profile_form':profile_form}
    return render(request,'socialapp/editprofile.html',context)

1 Answers1

2

You haven't supplied an instance of Profile when profile_form is instantiated the first time (on the method=GET path). This is how you get initial values to be the previously saved values.

@login_required
def editprofile(request):
    user_form=UserEditForm( instance = request.user)
    profile_form=ProfileEditForm( instance=request.user.profile)
    if request.method == POST:
       ...
nigel222
  • 7,582
  • 1
  • 14
  • 22