Change user information

Im creating a profile page where the user can update their information such as first name, last name and email address. How do I make it so that when the user lands on the profile page, by default the first name and last name is set to their previous first name and last name.

@login_required
def profile(request):
    if request.method == "POST":
        update_form = updateUserInformation(request.POST, instance=request.user)
        if update_form.is_valid():
            update_form.save()
            return redirect('profile')
    else:
        update_form = updateUserInformation()       
    return render(request, 'profile.html', {'update_form': update_form})

class updateUserInformation(forms.ModelForm):
    first_name = forms.CharField(required=False,max_length=150, widget=forms.TextInput(attrs={'placeholder': 'New First Name'}))
    last_name = forms.CharField(required=False,max_length=150, widget=forms.TextInput(attrs={'placeholder': ' New Last Name'}))
    email = forms.EmailField(required=False,widget=forms.TextInput(attrs={'placeholder': 'example@gmail.com'}))
    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email']
    


Pass the instance parameter in the form creation for the GET processing in addition to passing it into the form for the POST.

1 Like