如何使用不同的视图进行django注册? [英] How to use different view for django-registration?

查看:138
本文介绍了如何使用不同的视图进行django注册?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让django注册使用ViewsFormUniqueEmail视图,并按照 django注册问题。我将urls.py设置为

I have been trying to get django-registration to use the view RegistrationFormUniqueEmail and following the solution from this django-registration question. I have set my urls.py to

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

from registration.forms import RegistrationFormUniqueEmail

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    (r'^users/', include('registration.backends.default.urls')),
    url(r'^users/register/$', 'registration.backends.default.views.RegistrationView',
        {'form_class': RegistrationFormUniqueEmail,
         'backend': 'registration.backends.default.DefaultBackend'},       
        name='registration_register'),
)

我仍然可以使用相同的电子邮件创建多个帐户。问题是什么?不应该django注册使用我指定的视图?我正在使用django-registration 0.9b1。

However, I can still create multiple accounts with the same email. What is the problem? Shouldn't django-registration be using the view that I specified? I am currently using django-registration 0.9b1.

推荐答案

您正在使用的Django注册版本已被重写为使用类观点。这意味着您的urls.py需要一种不同的方法。

The version of Django registration you are using has been rewritten to use class based views. This means a different approach is required in your urls.py.

首先,您需要对RegistrationView进行子类化,并设置自定义表单类。

First, You need to subclass the RegistrationView, and set the custom form class.

from registration.backends.default.views import RegistrationView
from registration.forms import RegistrationFormUniqueEmail

class RegistrationViewUniqueEmail(RegistrationView):
    form_class = RegistrationFormUniqueEmail

然后,在您的网址中使用您的自定义RegistrationViewUniqueEmail子类。与其他基于类的视图一样,您必须调用as_view()。

Then, use your custom RegistrationViewUniqueEmail subclass in your urls. As with other class based views, you must call as_view().

url(r'^user/register/$', RegistrationViewUniqueEmail.as_view(),
                    name='registration_register'),

确保您的自定义registration_register视图之前包含默认注册网址,否则不会使用。

Make sure your customised registration_register view comes before you include the default registration urls, otherwise it won't be used.

这篇关于如何使用不同的视图进行django注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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