Django 1.5:django-registration电子邮件是一个独特的领域?

时间:2013-05-23 15:11:45

标签: django django-registration

我使用Django 1.5 + django-registration 0.9 ...

如何在模型用户中使电子邮件字段唯一?

from registration.forms import RegistrationFormUniqueEmail

url(r'^accounts/register/$', 'registration.views.register',
    {'form_class': RegistrationFormUniqueEmail,
     'backend': 'registration.backends.default.DefaultBackend'},       
     name='registration_register'),

此解决方案不适合

Could not import registration.views.register. View does not exist in module registration.views.

3 个答案:

答案 0 :(得分:5)

非常感谢,@ Alasdair How to use different view for django-registration?

urls.py:

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

class RegistrationViewUniqueEmail(RegistrationView):
    form_class = RegistrationFormUniqueEmail

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

答案 1 :(得分:3)

另一种方法是将表单类直接传递给as_view()方法,如下例所示:

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

答案 2 :(得分:0)

完整的urls.py:

from django.conf.urls import patterns, include, url
from registration.forms import RegistrationFormUniqueEmail
from registration.backends.default.views import RegistrationView

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'django_registration_demo.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/', include('django.contrib.auth.urls')),
    # enable unique email registration feature
    url(r'^accounts/register/$',
        RegistrationView.as_view(form_class=RegistrationFormUniqueEmail),
        name='registration_register'),
    url(r'^accounts/', include('registration.backends.default.urls'))
)

我还为django-registration创建了一个完整的演示,它启用了独特的电子邮件功能,请参阅: https://github.com/xiaohanyu/django-registration-demo