[Solved]-How to redirect users to a specific url after registration in django registration?

18πŸ‘

Don’t change the code in the registration module. Instead, subclass the RegistrationView, and override the get_success_url method to return the url you want.

from registration.backends.simple.views import RegistrationView

class MyRegistrationView(RegistrationView):
    def get_success_url(self, request, user):
        return "/upload/new"

Then include your custom registration view in your main urls.py, instead of including the simple backend urls.

urlpatterns = [
    # your custom registration view
    url(r'^register/$', MyRegistrationView.as_view(), name='registration_register'),
    # the rest of the views from the simple backend
    url(r'^register/closed/$', TemplateView.as_view(template_name='registration/registration_closed.html'),
                          name='registration_disallowed'),
    url(r'', include('registration.auth_urls')),
]
πŸ‘€Alasdair

2πŸ‘

I am using django_registration 3.1 package. I have posted all 3 files (views.py urls.py forms.py) that are needed to use this package.
To redirect user to a custom url on successfull registration, create a view that subclasses RegistrationView. Pass in a success_url of your choice.

Views.py:

from django_registration.backends.one_step.views import RegistrationView
from django.urls import reverse_lazy
class MyRegistrationView(RegistrationView):
    success_url = reverse_lazy('homepage:homepage')  # using reverse() will give error

urls.py:

from django.urls import path, include
from django_registration.backends.one_step.views import RegistrationView
from core.forms import CustomUserForm
from .views import MyRegistrationView

app_name = 'core'
urlpatterns = [

    # login using rest api
    path('api/', include('core.api.urls')),

    # register for our custom class
    path('auth/register/', MyRegistrationView.as_view(
        form_class=CustomUserForm
    ), name='django_registration_register'),

    path('auth/', include('django_registration.backends.one_step.urls')),
    path('auth/', include('django.contrib.auth.urls'))

]

forms.py:

from django_registration.forms import RegistrationForm
from core.models import CustomUser


class CustomUserForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = CustomUser
πŸ‘€Aseem

1πŸ‘

You can set SIMPLE_BACKEND_REDIRECT_URL in settings.py.

settings.py

SIMPLE_BACKEND_REDIRECT_URL = '/upload/new'
πŸ‘€Sanchit Gupta

0πŸ‘

If you wish, you can modify the following file /usr/local/lib/python2.7/dist-packages/registration/backends/simple/urls.py, changing the path, for example:

Before modifying:

success_url = getattr (settings, 'SIMPLE_BACKEND_REDIRECT_URL', '/'),

After modifying:

success_url = getattr (settings, 'SIMPLE_BACKEND_REDIRECT_URL', '/upload/new'),

Regards.

Diego

πŸ‘€Diego Santos

Leave a comment