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

前端 未结 3 1978
灰色年华
灰色年华 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: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
    

提交回复
热议问题