代码之家  ›  专栏  ›  技术社区  ›  HenryM

子类django registration ActivationView总是产生“您提供的激活密钥无效”?

  •  0
  • HenryM  · 技术社区  · 6 年前

    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 但我不明白为什么

    1 回复  |  直到 6 年前
        1
  •  1
  •   HenryM    5 年前

    如果其他人尝试这个,这是我的问题(这是相当愚蠢的)。

    我已经定义了我的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')),
    ]