I'm trying to use django-registration
as the core process for registration at a site, but I'm also trying to set the email
to the username
and not actually activate the user
until admin have done something. Subclassed the views as follows:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django_registration.backends.activation.views import ActivationView, RegistrationView
# Create your views here.
from my_registration.forms import NoUsernameRegistrationForm
class MyActivationView(ActivationView):
def activate(self, *args, **kwargs):
username = self.validate_key(kwargs.get('activation_key'))
user = self.get_user(username)
user.is_active = False
user.save()
return user
class NoUsernameRegistrationView(RegistrationView):
form_class = NoUsernameRegistrationForm
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.utils.translation import ugettext_lazy as _
from django_registration.forms import RegistrationForm
attrs_dict = {'class': 'required'}
class NoUsernameRegistrationForm(RegistrationForm):
"""
Form for registering a new user account.
Requires the password to be entered twice to catch typos.
Subclasses should feel free to add any additional validation they
need, but should avoid defining a ``save()`` method -- the actual
saving of collected user data is delegated to the active
registration backend.
"""
username = forms.CharField(
widget=forms.EmailInput(attrs=dict(attrs_dict, maxlength=75)),
label=_("Email address"))
password1 = forms.CharField(
widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password"))
password2 = forms.CharField(
widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
label=_("Password (again)"))
email = forms.EmailField(
widget=forms.HiddenInput(),
required = False)
def __init__(self, *args, **kwargs):
super(NoUsernameRegistrationForm, self).__init__(*args, **kwargs)
self.initial['email'] = 'dummy_email@dummy.com'
def clean(self):
"""
Verify that the values entered into the two password fields
match. Note that an error here will end up in
``non_field_errors()`` because it doesn't apply to a single
field.
"""
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_("The two password fields didn't match."))
#Validate that the username / email address is not already in use.
try:
user = User.objects.get(username__iexact=self.cleaned_data['username'])
raise forms.ValidationError(_("A user with that email address already exists."))
except User.DoesNotExist:
self.cleaned_data['email'] = self.cleaned_data['username']
self.cleaned_data['username']
return self.cleaned_data
I always get the INVALID_KEY_MESSAGE
but I cannot understand why
Should anyone else try this, here was my issue (and it is pretty foolish).
I had defined my urls.py as :
urlpatterns = [
#override the django_registration activation view
url(r'^accounts/activate/(?P<activation_key>[-:\w]+)/$',
views.MyActivationView.as_view(),
name='django_registration_activate'),
#override form
url(r'^accounts/register/$',
views.NoUsernameRegistrationView.as_view(),
name='django_registration_register'),
#But use the other views
url(r'^accounts/', include('django_registration.backends.activation.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
]
The activation was failing because it was trying to then call /accounts/activation/complete/
and so was using "complete" as an activation_key
. It needed to be:
urlpatterns = [
url(r'^accounts/activate/complete/$',
TemplateView.as_view(
template_name='django_registration/activation_complete.html'
),
name='django_registration_activation_complete'),
#override the django_registration activation view
url(r'^accounts/activate/(?P<activation_key>[-:\w]+)/$',
views.MyActivationView.as_view(),
name='django_registration_activate'),
#override form
url(r'^accounts/register/$',
views.NoUsernameRegistrationView.as_view(),
name='django_registration_register'),
#But use the other views
url(r'^accounts/', include('django_registration.backends.activation.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
]