DJPython
DJPython

Reputation: 209

Django-registration, force unique e-mail

Can I force users to make unique e-mail addresses in django-registration?

Upvotes: 16

Views: 8546

Answers (8)

jhthompson
jhthompson

Reputation: 316

As of django-registration 3.1.2, I was able to solve this in an almost identical way as https://stackoverflow.com/a/19383392/10987661:

from django_registration.forms import RegistrationFormUniqueEmail
from django_registration.backends.activation.views import RegistrationView

urlpatterns = [
    ...
    path('accounts/register/',
        RegistrationView.as_view(
            form_class=RegistrationFormUniqueEmail
        ),
        name='django_registration_register',
    ),
    path('accounts/', include('django_registration.backends.activation.urls')),
    ...
]

Upvotes: 0

Deep 3015
Deep 3015

Reputation: 10075

For unique e-mail addresses in django-registration-redux 1.4.

In url.py add following

from registration.forms import RegistrationFormUniqueEmail

from registration.backends.default.views import RegistrationView

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

 url(r'^accounts/', include('registration.backends.default.urls'))

]

Upvotes: 1

seddonym
seddonym

Reputation: 17229

For later versions of django_registration (that use class-based views), you can do this:

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

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

Upvotes: 17

raidsan
raidsan

Reputation: 797

from rych's answer, I tested that the following runs ok - it only uses urls.py, you needn't write another cusotmer form.

from registration.forms import RegistrationFormUniqueEmail

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

Upvotes: 13

rych
rych

Reputation: 1187

As miku pointed out, you should simply use RegistrationFormUniqueEmail .

If you implement according to the documentation and bug report replies (as of mid-2011), you'll probably end up with an exception like:

TypeError at /accounts/register/
register() takes at least 2 non-keyword arguments (1 given)

your urlconf should look like this to properly specify this backend:

(r'^accounts/register/', 'registration.views.register' {'form_class':RegistrationFormUniqueEmail, 'backend':'registration.backends.default.DefaultBackend' }),
(r'^accounts/', include('registration.backends.default.urls')),

[ please excuse the additional answer, as this belongs as a comment to miku's correct answer; I don't have the privilege of commenting, but this tip may save at least a few people 15 minutes each, so is hopefully worth the forced faux-pas ]

Upvotes: 4

Asinox
Asinox

Reputation: 6865

forms.py

from registration.forms import RegistrationFormUniqueEmail

class RegistroPerfilForm(RegistrationFormUniqueEmail):
    first_name= forms.CharField(required=True)
    last_name= forms.CharField(required=True)
    kind__of_user= forms.CharField(widget=forms.RadioSelect(choices=TIPO))

Upvotes: 4

miku
miku

Reputation: 188064

django-registration has several forms included in the source – one is a RegistrationFormUniqueEmail, which might help you ...


P.S. You can adjust the form to use by changing the default backend or by implementing a custom one, where you return the appropriate form class, see: http://bitbucket.org/ubernostrum/django-registration/src/073835a4269f/registration/backends/default/init.py#cl-118

Upvotes: 5

Johannes Charra
Johannes Charra

Reputation: 29923

It should suffice to create your registration form from your user model. If the e-mail address is defined to be unique there, the form will output an error on submit for duplicate addresses.

Look here for details.

As Dominic points out, you'll not be able to do this with the built-in user profile. You'll have to extend it by creating your own user profile as described here and make it contain a unique e-mail address.

Upvotes: 2

Related Questions