title: How do i create a custom django backend for django-registration?
body: <p>I've had a read of this</p>
  <blockquote>
  <p><a href="http://docs.b-list.org/django-registration/0.8/backend-api.html" rel="nofollow noreferrer">http://docs.b-list.org/django-registration/0.8/backend-api.html</a></p>
  </blockquote>
  <p>and i've had a shot at making my own backend. I am doing this because I want to create a backend that disallows having the same email used for registration, and I wanted to change the email-error message. I also wanted to add in my own field!</p>
  <p>Here is what I came up with:</p>
  <pre><code>from django import forms
  from registration.forms import RegistrationForm
  from django.utils.translation import ugettext_lazy as _
  from django.contrib.auth.models import User
  from registration.forms import attrs_dict
  
  class customRegistrationForm(RegistrationForm):
      email2 = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
          maxlength=75)),
          label=_(&quot;Confirm email&quot;))
  
      def clean_email(self):
          &quot;&quot;&quot;
          Validate that the email is alphanumeric and is not already
          in use.
          &quot;&quot;&quot;
          try:
              email = User.objects.get(email__iexact=self.cleaned_data['email'])
          except User.DoesNotExist:
              return self.cleaned_data['email']
          raise forms.ValidationError(_(&quot;That email already exists - if you have forgotten your password, go to the login screen and then select \&quot;forgot password\&quot;&quot;))
  
      def clean(self):
          &quot;&quot;&quot;
          Verifiy that the values entered into the two email fields
          match. Note that an error here will end up in
          ``non_field_errors()`` because it doesn't apply to a single
          field.
  
          &quot;&quot;&quot;
          if 'email' in self.cleaned_data and 'email2' in self.cleaned_data:
              if self.cleaned_data['email'] != self.cleaned_data['email2']:
                  raise forms.ValidationError(_(&quot;The two email fields didn't match.&quot;))
          return super(RegistrationForm,clean)
  </code></pre>
  <p>The above goes in my <strong>init</strong>.py file (whetever that is)</p>
  <p>Then, I have in my urls.py code:</p>
  <pre><code>url(r'^accounts/register/$',
      register,
          { 'backend': 'myapp.forms.customRegistrationForm' },
      name='registration_register'),
  ... #other urls here!
  </code></pre>
  <p>Now, when i go to the /accounts/register page, I get the following error:</p>
  <blockquote>
  <p>AttributeError at /accounts/register/</p>
  <p>'customRegistrationForm' object has no attribute 'registration_allowed'</p>
  </blockquote>
  <p>Which is weird. It seems to be telling me I need a &quot;registration_allowed&quot; method added to my subclass. BUT, the subclass is a subclass of the RegistrationForm, which works fine and doesn't have that stuff defined... I know I <em>can</em> add these members in, but it seems to beat the purpose of extension, right?</p>
  <p>UPDATE</p>
  <p>Here is the code now that it works!</p>
  <p>I broke up the varying classes into varying <strong>init</strong>.py files in different folders - one called &quot;forms&quot; and one called &quot;backends&quot;, both of which sit in a folder &quot;djangoRegistration&quot; under my main project.</p>
  <p>/forms/<strong>init</strong>.py</p>
  <pre><code>from django import forms
  from registration.forms import RegistrationForm
  from django.utils.translation import ugettext_lazy as _
  from django.contrib.auth.models import User
  from registration.forms import attrs_dict
  
  class customRegistrationForm(RegistrationForm):
      def __init__(self, *args, **kw):
          super(RegistrationForm, self).__init__(*args, **kw)
          self.fields.keyOrder = [
              'username',
              'email',
              'email2',
              'password1',
              'password2'
          ]
  
      email2 = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
          maxlength=75)),
          label=_(&quot;Confirm email&quot;))
  
      def clean_email(self):
          &quot;&quot;&quot;
          Validate that the email is alphanumeric and is not already
          in use.
          &quot;&quot;&quot;
          try:
              email = User.objects.get(email__iexact=self.cleaned_data['email'])
          except User.DoesNotExist:
              return self.cleaned_data['email']
          raise forms.ValidationError(_(&quot;That email already exists - if you have forgotten your password, go to the login screen and then select \&quot;forgot password\&quot;&quot;))
  
      def clean(self):
          &quot;&quot;&quot;
          Verifiy that the values entered into the two email fields
          match. Note that an error here will end up in
          ``non_field_errors()`` because it doesn't apply to a single
          field.
  
          &quot;&quot;&quot;
          if 'email' in self.cleaned_data and 'email2' in self.cleaned_data:
              if self.cleaned_data['email'] != self.cleaned_data['email2']:
                  raise forms.ValidationError(_(&quot;The two email fields didn't match.&quot;))
          return super(RegistrationForm,clean)
  </code></pre>
  <p>/backends/<strong>init</strong>.py</p>
  <pre><code>from registration.backends.default import DefaultBackend
  from dumpstownapp.djangoRegistration.forms import customRegistrationForm
  
  class customDefaultBackend(DefaultBackend):
      def get_form_class(self, request):
          &quot;&quot;&quot;
          Return the default form class used for user registration.
  
          &quot;&quot;&quot;
          return customRegistrationForm
  </code></pre>
  <p>and finally, my urls.py just references the new backend:</p>
  <pre><code>url(r'^accounts/register/$',
      register,
          { 'backend': 'myapp.djangoRegistration.backends.customDefaultBackend' },
      name='registration_register'),
  #more urls here! yay!
  </code></pre>
  <p>As a final note, I had to add some code to &quot;order&quot; the way the fields were presented, which is what the <strong>init</strong> method in the customRegistrationForm is doing</p>
  <p>thanks!</p>