Search code examples
pythondjangoprofile

Django 1.6 - edit profile - AttributeError at /editseekerprofile/


I'm trying to create edit user profile functionality. But I get the following error

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/views.py" in update_profile
  60.             form.save()
File "/forms.py" in save
  108.         user = super(UserUpdateForm, self).save(commit=False)

Exception Type: AttributeError at /editseekerprofile/
Exception Value: 'super' object has no attribute 'save'

Here is my views.py

def update_profile(request):
    args = {}

    if request.method == 'POST':
        form = UserUpdateForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request,'meddy1/seekerprofile.html',{'doctorSeeker': profile})


    else:
        form = UserUpdateForm()

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

Here is my forms.py

class UserUpdateForm(forms.Form):
    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

Just to clarify, I'm saving user information in two different models, one is the default user model and the other one is DoctorSeeker.


Solution

  • forms.Form has no method called save, thats why you can't override it. forms.ModelForm has the save method which you can override.