Подклассы django-registration ActivationView всегда выдает «Ключ активации, который вы указали, недействителен.»? - PullRequest
0 голосов
/ 17 сентября 2018

Я пытаюсь использовать 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 Ответ

0 голосов
/ 18 сентября 2018

Если кто-нибудь еще попробует это, вот моя проблема (и это довольно глупо).

Я определил свой urls.py как:

urlpatterns = [
    #override the django_registration activation view
    url(r'^accounts/activate/(?P<activation_key>[-:\w]+)/$',
        views.MerzActivationView.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.MerzActivationView.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')),
]
...