Имя и фамилия в django-registration

  • Опубликовано:
По этой инструкции сделал собственный backend для django-registration.

Получилось практически сразу, правда пришлось исправить синтаксические ошибки (они как-будто преднамеренно вставлены... зачем?) На всякий случай цитирую свою версию наследованных классов.

# my_registration/backends/my_custom_backend/__init__.py

# Usually, my custom backend is going to inherit from DefaultBackend
from registration.backends.default import DefaultBackend

# Inside my custom backend, I will have to define 6 methods:
#        * register
#        * activate
#        * registration_allowed
#        * get_form_class
#        * post_registration_redirect
#        * post_activation_redirect
# You can change their behaviour or just keep them the way they are.
from accounts.models import InstructorProfile
from my_registration.forms import FullRegistrationForm

class MyCustomBackend(DefaultBackend):
    def register(self, request, **kwargs):
        user = super(MyCustomBackend, self).register(request, **kwargs)
        # Here I can get the additional fields from the new form I'm sending
        user.first_name = kwargs['first_name']
        user.last_name = kwargs['last_name']
        user.save()
        # I could also create a UserProfile for the new user
        profile = InstructorProfile()
        profile.user = user
        #profile.telephone = kwargs['telephone']
        profile.save()
        # Or I could do something else, e.g. send an email to the new user
        return user

        # If you don't want to change the behaviour of the method you still
        # have to define it, but just calling super (same for all 6 methods).

    def activate(self, request, activation_key):
        return super(MyCustomBackend, self).activate(request, activation_key)

    def registration_allowed(self, request):
        return super(MyCustomBackend, self).registration_allowed(request)

    def get_form_class(self, request):
        #return super(MyCustomBackend, self).get_form_class(request)
        return FullRegistrationForm

    def post_registration_redirect(self, request, user):
        return super(MyCustomBackend, self).post_registration_redirect(request, user)

    def post_activation_redirect(self, request, user):
        return super(MyCustomBackend, self).post_activation_redirect(request, user)
# my_registration/backends/my_custom_backend/urls.py

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template
from registration.views import activate, register

urlpatterns = patterns('',
    url(r'^activate/complete/$',
        direct_to_template,
        # You can change the template if you need to
        {'template': 'registration/activation_complete.html'},
        name='registration_activation_complete'),
    url(r'^activate/(?P\w+)/$',
        activate,
        # Here, we change the backend to our custom backend
        {'backend': 'my_registration.backends.my_custom_backend.MyCustomBackend'},
        name='registration_activate'),
    url(r'^register/$',
        register,
        # Here, in the registration part, we change the backend and, if
        # you need so, add the form to be used
        {'backend': 'my_registration.backends.my_custom_backend.MyCustomBackend'},
        name='registration_register'),
    url(r'^register/complete/$',
        direct_to_template,
        # You can change the template if you need to
        {'template': 'registration/registration_complete.html'},
        name='registration_complete'),
    url(r'^register/closed/$',
        direct_to_template,
        # You can change the template if you need to
        {'template': 'registration/registration_closed.html'},
        name='registration_disallowed'),
    (r'', include('registration.auth_urls')),
)
Наконец, в корневом urls.py указываем на наш новый бэкенд:
url(r'^accounts/', include('my_registration.backends.my_custom_backend.urls')),