0

I'm trying to create edit user profile functionality. But I get the following error. I feel like it's trying to create a new user instead of editing the existing one. So that might result into username not being unique. I've no idea what to change/add here.

Here is my views

def showProfile(request, id):
    profile = DoctorSeeker.objects.get(id=id)
    return render(request,'meddy1/seekerprofile.html',{'doctorSeeker': profile})

def update_profile(request):
    args = {}

    if request.method == 'POST':
        form = UserUpdateForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, showProfile())

    else:
        form = UserUpdateForm()

    args['form'] = form
    return render(request, 'meddy1/editseekerprofile.html', args)

Here is my forms

class UserUpdateForm(forms.ModelForm):
    name = forms.CharField(max_length=200, widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'FirstName LastName'}))
    email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Please enter a valid email address so we can reach you. No spam. Ever.'}))

    password1 = forms.CharField(label="Old Password",widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder':'Enter your password to save the changes.'}),required=False)
    password2 = forms.CharField(label="New Password?",widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder':'Only enter new password if you want to change it.'}),required=False)
    password3 = forms.CharField(label="Confirm New Password",widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder':'Confirm New Password'}),required=False)



    class Meta:
        model = DoctorSeeker
        fields = ("name","email")

    class Meta:
        model = User
        fields = ("password2", "password3")

    def clean_password2(self):
        password2 = self.cleaned_data.get("password2")
        password3 = self.cleaned_data.get("password3")
        if password2 and password3 and password2 != password3:
            raise forms.ValidationError(
                self.error_messages['password_mismatch'],
                code='password_mismatch',
            )
        return password2

    def save(self, commit=True):
        user = super(UserUpdateForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password2"])
        fullName = self.cleaned_data["name"]
        Email = self.cleaned_data["email"]

    if commit:
        user.save()
        userProfile = DoctorSeeker(user=user, name=fullName, email=Email)
        userProfile.save()

    return user
James L.
  • 1,143
  • 22
  • 52

1 Answers1

1

the save method of django form will create a new object (if no instance are suplied) and not an update of this one. You have to retreive your user first pass the instance through the form and then save it.

For more informations see: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method

Freelancer
  • 4,459
  • 2
  • 22
  • 28