Nick
Nick

Reputation: 1541

Django Registration form Verification Email

I originally started out by using django-registration to implement registration email verification. I've implemented a custom login/registration form to my django app to allow users to login with a unique email address. However, doing so, overrode my django-registration workflow.

Ideally i would like to send the user a verification email after the user registers - instead of redirecting them to the login page. I'm not sure if this has something to do with the settings.py file or not.

models.py

class AccountUserManager(UserManager):
    def _create_user(self, username, email, password,
                     is_staff, is_superuser, **extra_fields):
        """
        Creates and saves a User with the given username, email and password.
        """
        now = timezone.now()
        if not email:
            raise ValueError('The given username must be set')

        email = self.normalize_email(email)
        user = self.model(username=email, email=email,
                          is_staff=is_staff, is_active=True,
                          is_superuser=is_superuser,
                          date_joined=now, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)

        return user

class User(AbstractUser):
    # now that we've abstracted this class we can add any
    # number of custom attribute to our user class

    # in later units we'll be adding things like payment details!

    objects = AccountUserManager()

forms.py

class UserRegistrationForm(UserCreationForm):
    email = forms.EmailField(
        label='',
        widget=forms.EmailInput(attrs={'placeholder': 'Email Address'})
    )
    password1 = forms.CharField(
        label='',
        widget=forms.PasswordInput(attrs={'placeholder': 'Password'})
    )

    password2 = forms.CharField(
        label='',
        widget=forms.PasswordInput(attrs={'placeholder': 'Confirm Password'})
    )

    class Meta:
        model = User
        fields = ['email', 'password1', 'password2']
        exclude = ['username']

    def clean_password2(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')

        if password1 and password2 and password1 != password2:
            message = "Passwords do not match"
            raise ValidationError(message)

        return password2

    def save(self, commit=True):
        instance = super(UserRegistrationForm, self).save(commit=False)

        # automatically set to email address to create a unique identifier
        instance.username = instance.email

        if commit:
            instance.save()

        return instance

views.py

def register(request, register_form=UserRegistrationForm):
    if request.method == 'POST':
        form = register_form(request.POST)
        if form.is_valid():
            form.save()

            user = auth.authenticate(email=request.POST.get('email'),
                                     password=request.POST.get('password1'))

            if user:
                messages.success(request, "You have successfully registered")
                return redirect(reverse('profile'))

            else:
                messages.error(request, "unable to log you in at this time!")

    else:
        form = register_form()

    args = {'form':form}
    args.update(csrf(request))

    return render(request, 'register.html', args)

Upvotes: 0

Views: 2344

Answers (1)

Christoph Bluoss
Christoph Bluoss

Reputation: 359

I'm not sure if you still want to use django-registration or not?

If you still want to use django-registration, HMAC authentification is documented here.

if not you need to send a mail by yourself. (e.g. with django.core.mail) before returning the rendered template or a redirect.

Upvotes: 1

Related Questions