How to redirect users to a specific url after registration in django registration?

前端 未结 3 1975
灰色年华
灰色年华 2020-12-16 02:11

So I am using django-registration app to implement a user registration page for my site. I used Django\'s backends.simple views which allows the users to immediately login a

相关标签:
3条回答
  • 2020-12-16 02:20

    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

    0 讨论(0)
  • 2020-12-16 02:31

    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')),
    ]
    
    0 讨论(0)
  • 2020-12-16 02:37

    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
    
    0 讨论(0)
提交回复
热议问题