在django注册后,如何将用户重定向到特定的url? [英] How to redirect users to a specific url after registration in django registration?

查看:128
本文介绍了在django注册后,如何将用户重定向到特定的url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用django注册应用来实现我的网站的用户注册页面。我使用Django的backends.simple视图,允许用户在注册后立即登录。我的问题是如何将它们重定向到与项目相同的目录中的其他应用程序的页面。

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 after registration. My question is how do I redirect them to my other app's page located in the same directory as the project.

这是我的主要urls.py看起来像:

Here is what my main urls.py looks like:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',

    url(r'^accounts/', include('registration.backends.simple.urls')),
    url(r'^upload/', include('mysite.fileupload.urls')),
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^mysite/', include('mysite.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
)

import os
urlpatterns += patterns('',
        (r'^media/(.*)$', 'django.views.static.serve', {'document_root': os.path.join(os.path.abspath(os.path.dirname(__file__)), 'media')}),
)

fileupload是我在项目目录中的其他应用程序的名称mysite。

fileupload is the name of the other app I have in the project directory mysite.

这是backends.simple.urls的样子:

This is what the backends.simple.urls looks like:

"""
URLconf for registration and activation, using django-registration's
one-step backend.

If the default behavior of these views is acceptable to you, simply
use a line like this in your root URLconf to set up the default URLs
for registration::

    (r'^accounts/', include('registration.backends.simple.urls')),

This will also automatically set up the views in
``django.contrib.auth`` at sensible default locations.

If you'd like to customize registration behavior, feel free to set up
your own URL patterns for these views instead.

"""


from django.conf.urls import include
from django.conf.urls import patterns
from django.conf.urls import url
from django.views.generic.base import TemplateView

from registration.backends.simple.views import RegistrationView


urlpatterns = patterns('',
                       url(r'^register/$',
                           RegistrationView.as_view(),
                           name='registration_register'),
                       url(r'^register/closed/$',
                           TemplateView.as_view(template_name='registration/registration_closed.html'),
                           name='registration_disallowed'),
                       (r'', include('registration.auth_urls')),
                       )

这里是backends.simple.views:

And here is the backends.simple.views:

from django.conf import settings
from django.contrib.auth import authenticate
from django.contrib.auth import login
from django.contrib.auth.models import User

from registration import signals
from registration.views import RegistrationView as BaseRegistrationView


class RegistrationView(BaseRegistrationView):
    """
    A registration backend which implements the simplest possible
    workflow: a user supplies a username, email address and password
    (the bare minimum for a useful account), and is immediately signed
    up and logged in).

    """
    def register(self, request, **cleaned_data):
        username, email, password = cleaned_data['username'], cleaned_data['email'], cleaned_data['password1']
        User.objects.create_user(username, email, password)

        new_user = authenticate(username=username, password=password)
        login(request, new_user)
        signals.user_registered.send(sender=self.__class__,
                                     user=new_user,
                                     request=request)
        return new_user

    def registration_allowed(self, request):
        """
        Indicate whether account registration is currently permitted,
        based on the value of the setting ``REGISTRATION_OPEN``. This
        is determined as follows:

        * If ``REGISTRATION_OPEN`` is not specified in settings, or is
          set to ``True``, registration is permitted.

        * If ``REGISTRATION_OPEN`` is both specified and set to
          ``False``, registration is not permitted.

        """
        return getattr(settings, 'REGISTRATION_OPEN', True)

    def get_success_url(self, request, user):
        return (user.get_absolute_url(), (), {})

我尝试将get_success_url函数更改为返回我想要的URL / / upload / new,但仍然将我重定向到用户/ 插入用户名页面,并发出错误。如何将用户重定向到其他应用程序的上传/新页面注册后驻留?

I tried the changing the get_success_url function to just return the url I want which is /upload/new but it still redirected me to users/insert username page and gave an error. How do I redirect the user to the upload/new page where the other app resides after registration?

推荐答案

不要更改注册中的代码模块,而是将 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')),
]

这篇关于在django注册后,如何将用户重定向到特定的url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆