Josh Laird Josh Laird - 1 month ago 7
Python Question

Django-Registration-Redux: overriding default form and success URL

I've got

django-registration-redux
working but when trying to use a custom form and setting a custom success_url, it does not do it. If I change the source code for the
BaseRegistrationView
then it will work, why is this happening? I believe I am overriding correctly.

registration
is at the top of my INSTALLED_APPS. I have migrated.

forms.py

from django import forms
from registration.forms import RegistrationFormUniqueEmail


class UserProfileRegistrationForm(RegistrationFormUniqueEmail):
field = forms.CharField()


URLs.py

import logging
logging.basicConfig(filename='example.log', level=logging.DEBUG)

from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.contrib import admin
from registration.backends.simple.views import RegistrationView

from myapp.models import UserProfile
from myapp.forms import UserProfileRegistrationForm


class MyRegistrationView(RegistrationView):
logging.debug("Class initialised")
success_url = '/test/'
form_class = UserProfileRegistrationForm

def register(self, form_class):
logging.debug("Registering")
new_user = super(MyRegistrationView, self).register(form_class)
user_profile = UserProfile()
user_profile.user = new_user
user_profile.field = form_class.cleaned_data['field']
user_profile.save()
logging.debug(user_profile)
return user_profile

def get_form_class(self):
logging.debug("Getting form class")
return UserProfileRegistrationForm


urlpatterns = [
url(r'^', include('myapp.urls')),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^accounts/register/$', MyRegistrationView.as_view(form_class=UserProfileRegistrationForm), name="registration_register"),
url(r'^accounts/password/change/$', MyRegistrationView.as_view(), name="auth_password_change"),
url(r'^accounts/password/change/done/$', MyRegistrationView.as_view(), name="auth_password_changed"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


The logging for "Registering" never gets hit and neither does the "Getting form class".

I have tried all 5 possible imports for
RegistrationView
:

registration.views.RegistrationView
registration.backends.hmac.views.RegistrationView
registration.backends.simple.views.RegistrationView
registration.backends.default.views.RegistrationView
registration.backends.model_activation.views.RegistrationView

Answer

You need to include your custom views before you include the default urls. Otherwise Django will match the default url first, and your custom view will never be used.

url(r'^accounts/register/$', MyRegistrationView.as_view(form_class=UserProfileRegistrationForm), name="registration_register"),
...
url(r'^accounts/', include('registration.backends.simple.urls')),
Comments