子类化 django-registration ActivationView 总是产生 "The activation key you provided is invalid."?
subclassing django-registration ActivationView is always producing "The activation key you provided is invalid."?
我正在尝试使用 django-registration
作为网站注册的核心流程,但我也在尝试将 email
设置为 username
而不是激活 user
直到管理员完成某些操作。将视图细分如下:
# -*- 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
我总是得到 INVALID_KEY_MESSAGE
但我不明白为什么
如果其他人尝试这样做,这是我的问题(这很愚蠢)。
我将 urls.py 定义为:
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')),
]
激活失败,因为它随后尝试调用 /accounts/activation/complete/
,因此使用 "complete" 作为 activation_key
。它必须是:
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')),
]
我正在尝试使用 django-registration
作为网站注册的核心流程,但我也在尝试将 email
设置为 username
而不是激活 user
直到管理员完成某些操作。将视图细分如下:
# -*- 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
我总是得到 INVALID_KEY_MESSAGE
但我不明白为什么
如果其他人尝试这样做,这是我的问题(这很愚蠢)。
我将 urls.py 定义为:
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')),
]
激活失败,因为它随后尝试调用 /accounts/activation/complete/
,因此使用 "complete" 作为 activation_key
。它必须是:
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')),
]