macropin / django-registration

Django-registration (redux) provides user registration functionality for Django websites.
http://django-registration-redux.readthedocs.org
Other
975 stars 350 forks source link

Migrating from django-registration #384

Closed caseydm closed 4 years ago

caseydm commented 4 years ago

Hi all. I'm migrating from regular django-registration (not redux) in order to get HTML email support and the ability to resend activation emails. Great work on this by the way!

I have a custom user model so need to subclass the registration view. But I am getting a NotImplementedError when I try to register a new user. The error points to the register method.

  def register(self, form):
        """
        Implement user-registration logic here.
        """
        raise NotImplementedError 

Here is my code:

# views.py
from registration.views import RegistrationView, ActivationView
from django.urls import reverse_lazy
from users.forms import CustomRegistrationForm

class CustomRegistrationView(RegistrationView):
    template_name = 'registration.html'
    form_class = CustomRegistrationForm
    success_url = reverse_lazy('activation_sent')

class CustomActivationView(ActivationView):
    template_name = 'activation_failed.html'
    success_url = reverse_lazy('registration_complete')

# forms.py
from django import forms
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from registration.forms import RegistrationForm
from users.models import User

class CustomRegistrationForm(RegistrationForm):
    first_name = forms.CharField(max_length=30, label='First name')
    last_name = forms.CharField(max_length=30, label='Last name')

    class Meta:
        model = User
        fields = ('email', 'first_name', 'last_name')

class CustomUserCreationForm(UserCreationForm):

    class Meta:
        model = User
        fields = ('email',)

class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = User
        fields = ('email',)

# models.py
from django.conf import settings
from django.db import models
from django.contrib.auth.models import AbstractUser, BaseUserManager

class UserManager(BaseUserManager):
    def create_user(self, email, password):
        user = self.model(email=self.normalize_email(email))
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        user = self.create_user(email, password)
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user

class User(AbstractUser):
    email = models.EmailField('email address', unique=True)
    first_name = models.CharField('first name', max_length=30, blank=True, null=True)
    last_name = models.CharField('last name', max_length=150, blank=True, null=True)
    username = models.CharField('username', max_length=150, blank=True, null=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    objects = UserManager()

    class Meta:
        verbose_name = 'user'
        verbose_name_plural = 'users'

Anybody see what I am doing wrong?

macropin commented 4 years ago

Is this a trick question, or have you not implemented register()?

caseydm commented 4 years ago

No not a trick question. I thought I did not have to implement register() since it is subclassed. But I guess I will try and do that.

macropin commented 4 years ago

Just use one of the example backends if it suits your usecase https://github.com/macropin/django-registration/blob/master/registration/backends/simple/views.py#L19 this is also covered in the docs https://django-registration-redux.readthedocs.io/en/latest/simple-backend.html