Bob
Bob

Reputation: 339

Django update user email address

I'd like to allow my users to change their emails. Is there a plugin to allow me to do this? I tried "django-change-email 0.1.2", but it doesn't appear to work. I've fixed some outdated errors, and it asked to update the database after that. I did so, but the database didn't appear to show any new tables to change the email.

Basically, I'd like for users to update their email address themselves. The server will then send a confirmation email containing a unique hash. Clicking on this will validate the change and save the email. Is this possible with some other plugin? Thank you!

Upvotes: 1

Views: 8014

Answers (1)

pass-by-ref
pass-by-ref

Reputation: 1298

The best way to allow users to change their email address is to create a separate UserProfile models which can be used to store email address. Example code is shown below.

class UserProfileForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(UserProfileForm, self).__init__(*args, **kwargs)
        try:
            self.fields['email'].initial = self.instance.user.email
        except User.DoesNotExist:
            pass

    email = forms.EmailField(label="Primary email")

    class Meta:
        model = Parent

def save(self, *args, **kwargs):
  """
  Update the primary email address on the related User object as well. 
  """
  u = self.instance.user
  u.email = self.cleaned_data['email']
  u.save()
  profile = super(UserProfileForm, self).save(*args,**kwargs)
  return profile

This way, you can ensure that the new email address remains inactive until the user has clicked on verify email address link which you will be sending to the user. Hope I answered your question.

Upvotes: 1

Related Questions