8

Vì vậy, tôi đang sử dụng ứng dụng đăng ký django để triển khai trang đăng ký người dùng cho trang web của tôi. Tôi đã sử dụng chế độ xem backends.simple của Django cho phép người dùng đăng nhập ngay sau khi đăng ký. Câu hỏi của tôi là làm thế nào để chuyển hướng chúng đến trang của ứng dụng khác của tôi nằm trong cùng thư mục với dự án.Cách chuyển hướng người dùng đến một url cụ thể sau khi đăng ký trong đăng ký django?

Đây là những gì urls.py chính của tôi trông giống như:

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 là tên của ứng dụng khác mà tôi có trong thư mục mysite dự án.

Đây là những gì backends.simple.urls trông giống như:

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

Và đây là 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(),(), {}) 

Tôi đã cố gắng thay đổi chức năng get_success_url chỉ trở lại url tôi muốn là/upload/new nhưng nó vẫn chuyển hướng tôi đến người dùng/chèn tên người dùng trang và đưa ra lỗi. Làm cách nào để chuyển hướng người dùng đến trang tải lên/trang mới nơi ứng dụng khác cư trú sau khi đăng ký?

Trả lời

17

Không thay đổi mã trong mô-đun registration. Thay vào đó, hãy phân lớp RegistrationView và ghi đè phương thức get_success_url để trả về url bạn muốn.

from registration.backends.simple.views import RegistrationView 

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

Sau đó, bao gồm quan điểm đăng ký tuỳ chỉnh của bạn trong chính bạn urls.py, thay vì bao gồm các url backend đơn giản.

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

Tôi đặt lớp MyRegistrationView ở đâu chính xác? Nó có đi trên views.py trong thư mục backend.simple hoặc thư mục bên trong thư mục đăng ký không? – user2476295

+1

Như tôi đã nói ở trên, không thay đổi mã trong mô-đun đăng ký. Nó sẽ làm cho nó nhiều, khó khăn hơn nhiều để nâng cấp lên phiên bản tiếp theo của django-đăng ký. Bạn có thể đặt lớp học vào một trong các view.py của ứng dụng của riêng bạn. Bạn có thể bao gồm nó trong 'urls.py' nếu bạn thích, vì nó chỉ dài ba dòng. – Alasdair

+0

Vì vậy, tôi đã thử điều này tôi đặt cả hai khối mã trong backends.simple.urls.py Vì, tệp urls.py chính trong thư mục dự án của tôi thực hiện cuộc gọi đến tệp simple.urls như bạn có thể thấy từ trên.Nó vẫn không hoạt động và chuyển hướng đến trang người dùng/tên người dùng ngay sau khi đăng ký (không phải đăng nhập) – user2476295

0

Nếu bạn muốn, bạn có thể sửa đổi các tập tin sau đây /usr/local/lib/python2.7/dist-packages/registration/backends/simple/urls.py, thay đổi đường dẫn, ví dụ :

Trước khi thay đổi:

success_url = getattr (cài đặt, 'SIMPLE_BACKEND_REDIRECT_URL', '/'),

Sau khi sửa đổi:

success_url = getattr (cài đặt, 'SIMPLE_BACKEND_REDIRECT_URL', '/ upload/mới'),

trọng.

Diego

Các vấn đề liên quan