Question

I am using django-inspectional-registration and I would like to extend the RegistrationView

class RegistrationView(FormMixin, TemplateResponseMixin, ProcessFormView):
"""A complex view for registration

GET:
    Display an RegistrationForm which has ``username``, ``email1`` and ``email2``
    for registration.
    ``email1`` and ``email2`` should be equal to prepend typo.

    ``form`` and ``supplement_form`` is in context to display these form.

POST:
    Register the user with passed ``username`` and ``email1``
"""
template_name = r'registration/registration_form.html'

def __init__(self, *args, **kwargs):
    self.backend = get_backend()
    super(RegistrationView, self).__init__(*args, **kwargs)

I did the following in my views.py:

def extended_registration(request, *args, **kwargs):
    k = 1+1

    return RegistrationView(request, *args, **kwargs)

RegistrationView = extended_registration(RegistrationView)

It seems that the created decorator is working but I get:

Traceback:
File "/Users/my_environment/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  92.                     response = middleware_method(request)
File "/Users/my_environment/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/Users/myProject/urls.py" in <module>
  5. from myProject.views import *
File "/Users/myProject/views.py" in <module>
  92. RegistrationView = extended_registration(RegistrationView)
File "/Users/myProject/views.py" in extended_registration
  90.     return RegistrationView(request, *args, **kwargs)
File "/Users/myProject/registration/views.py" in __init__
  157.         super(RegistrationView, self).__init__(*args, **kwargs)

Exception Type: TypeError at /accounts/register/complete/
Exception Value: __init__() takes exactly 1 argument (2 given)
Was it helpful?

Solution

You extend the class based view by using inheritance:

class MyRegistrationView(RegistrationView):
    def __init__(self, *args, **kwargs):
        k = 1 + 1
        super(MyRegistrationView, self).__init__(*args, **kwargs)

then define custom route:

url(r'^registration/register/$', MyRegistrationView.as_view(),
    name='registration_register'),
url('^registration/', include('registration.urls')),

the order is important! Our custom route need to be defined before registration app routes.

OTHER TIPS

For other's reference:

I came across this when I was looking to extend django-inspectional-registration's user model, then I found that there is a suggested method for doing this in their docs.

To extend the view with RegistrationView, you first need to import it:

from registration.views import RegistrationView
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top