2013-07-17 136 views
8

所以我使用django注册应用程序来实现我的网站的用户注册页面。我使用了Django的后端简单视图,允许用户在注册后立即登录。我的问题是如何将它们重定向到与项目位于同一目录中的其他应用程序的页面。如何在django注册后注册后将用户重定向到特定的url?

这里是我的主urls.py是什么样子:

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

文件上传是其他应用程序,我必须在项目目录的mysite的名字。

这是backends.simple.urls看起来像什么:

""" 
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:

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功能,只是回报我想要的网址是/ upload/new,但它仍然将我重定向到用户/ 插入用户名页面并出现错误。如何在注册后将用户重定向到其他应用程序所在的上传/新页面?

回答

17

不要更改registration模块中的代码。相反,子类RegistrationView,并覆盖get_success_url方法返回你想要的网址。

from registration.backends.simple.views import RegistrationView 

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

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

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

我到底在哪儿放了MyRegistrationView类?它是继续在backend.simple文件夹中的views.py还是注册文件夹中的views.py? – user2476295

+1

正如我上面所说的,不要更改注册模块中的代码。如果升级到下一个版本的django注册,它会变得非常困难。您可以将该类放入您自己的应用程序的views.py中。如果您愿意,可以将它包含在'urls.py'中,因为它只有三行。 – Alasdair

+0

所以我试了这个,我把这两个代码块放在backends.simple.urls.py中因为我的项目文件夹中的主要urls.py文件调用了simple.urls,就像你从上面看到的那样。它仍然不起作用,并在注册后立即重定向到用户/用户名页面(不是登录) – user2476295

0

如果你愿意,你可以修改下列文件/usr/local/lib/python2.7/dist-packages/registration/backends/simple/urls.py,改变路径,例如:

之前修改:

success_url = GETATTR(设置, 'SIMPLE_BACKEND_REDIRECT_URL', '/'),

修改后:

success_url = GETATTR(设置, 'SIMPLE_BACKEND_REDIRECT_URL', '/上传/新'),

问候。

Diego