且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在django注册后,如何将用户重定向到特定的url?

更新时间:2023-12-01 23:42:46

不要更改注册中的代码模块,而是将 RegistrationView 子类,并覆盖 get_success_url 方法以返回所需的URL。

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"

然后在您的主要网址中包含您的自定义注册视图。 py ,而不是包含简单的后端网址。

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')),
]