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

在django注册中注册后,如何将用户重定向到特定url?,django,django-templates,django-views,django-registration,Django,Django Templates,Django Views,Django Registration,所以我使用django注册应用程序为我的网站实现了一个用户注册页面。我使用了Django的backends.simple视图,它允许用户在注册后立即登录。我的问题是如何将它们重定向到与项目位于同一目录下的其他应用程序页面 以下是my main URL.py的外观: from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: # from djang

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

以下是my main URL.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')}),
)
"""
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')),
                       )
fileupload是我在项目目录mysite中拥有的另一个应用程序的名称

这就是backends.simple.url的外观:

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')}),
)
"""
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函数以仅返回我想要的url,即/upload/new,但它仍将我重定向到用户/插入用户名页面,并给出了一个错误。注册后,如何将用户重定向到其他应用所在的上载/新建页面

不要更改注册模块中的代码。相反,将
RegistrationView
子类化,并覆盖
get\u success\u url
方法以返回所需的url

from registration.backends.simple.views import RegistrationView

class MyRegistrationView(RegistrationView):
    def get_success_url(self, request, user):
        return "/upload/new"
然后在主
url.py
中包含自定义注册视图,而不是包含简单的后端URL

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

如果愿意,可以修改以下文件/usr/local/lib/python2.7/dist-packages/registration/backends/simple/url.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
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'))

]
from django_registration.forms import RegistrationForm
from core.models import CustomUser


class CustomUserForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = CustomUser
success\u url=getattr(设置为“简单\u后端\u重定向\u url',“/”)

修改后:

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

]
from django_registration.forms import RegistrationForm
from core.models import CustomUser


class CustomUserForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = CustomUser
success_url=getattr(设置为“简单_后端_重定向_url”,“/upload/new”)

问候


Diego

我正在使用django_注册3.1软件包。我已经发布了使用此软件包所需的所有3个文件(views.py url.py forms.py)。 若要在成功注册时将用户重定向到自定义url,请创建一个将RegistrationView子类化的视图。传递您选择的成功url

视图。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
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'))

]
from django_registration.forms import RegistrationForm
from core.models import CustomUser


class CustomUserForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = CustomUser
url.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
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'))

]
from django_registration.forms import RegistrationForm
from core.models import CustomUser


class CustomUserForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = CustomUser
forms.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
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'))

]
from django_registration.forms import RegistrationForm
from core.models import CustomUser


class CustomUserForm(RegistrationForm):
    class Meta(RegistrationForm.Meta):
        model = CustomUser

我应该把类MyRegistrationView放在哪里?它是放在backend.simple文件夹中的views.py上还是注册文件夹中的views.py上?正如我上面所说的,不要更改注册模块中的代码。这将使升级到下一版本的django注册变得越来越困难。您可以将该类放在自己应用程序的views.py中。如果愿意,您可以将其包含在
urls.py
中,因为它只有三行长。所以我尝试了这个方法,我将两个代码块都放在backends.simple.urls.py中,因为我的项目文件夹中的主url.py文件调用simple.urls,正如您从上面看到的那样。它仍然不起作用,并在注册(非登录)后立即重定向到用户/用户名页面。我已经说过两次,不要在注册模块中编辑代码。由于您仍在尝试编辑backends.simple.urls.py,我不能再帮您了。我知道我不应该在注册模块中修改代码,但是,我在backends.simple.urls.py中所做的更改应该会使它正常工作?让我按你的方式试试,然后再打给你。非常感谢你的帮助