9

Django-Registration has several form classes in the forms.py file. One is "class RegistrationFormTermsOfService(RegistrationForm) ..

What do I change in the rest of Django Registration code to enable this form in my registration flow instead of RegistrationForm?

Brenden
  • 8,264
  • 14
  • 48
  • 78

5 Answers5

12

Updating the accepted answer to conform with Django 1.5 and the latest version of django-registration:

in urls.py:

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

urlpatterns = patterns('',
    url(r'^accounts/register/$', RegistrationView.as_view(form_class=RegistrationFormTermsOfService), name='registration_register'),
    # your other URLconf stuff follows ...
)

then update the registration_form.html template and add a tos field, e.g.:

<p>
<label for="id_tos">I accept the terms of service</label>
{% if form.tos.errors %}
    <p class="errors">{{ form.tos.errors.as_text }}</p>
{% endif %}
{{ form.tos }}
</p>
  • if you want to use `simple` workflow which requires no email activation and the user is activated immediately , import this view instead `from.registration.backends.simple.views import RegistrationView`. – Ayed Jul 22 '16 at 08:29
6

You can simply go into your urls.py and override the form class by doing something like:

from registration.forms import RegistrationFormTermsOfService

(r'^accounts/register/$', 'registration.views.register', {'form_class' : RegistrationFormTermsOfService}),
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Abid A
  • 7,588
  • 4
  • 32
  • 32
  • that sounds amazingly simple ... and the new form should show up on reload of the reg form? – Brenden Jun 28 '11 at 07:15
  • Why don't you give it a try :-) (Hint: yes, it should) – Abid A Jun 28 '11 at 21:30
  • So I'm putting this in my main urls.py, and before the registration line. But I'm getting the error register() takes at least 2 non-keyword arguments (1 given) – Brenden Jul 04 '11 at 21:51
  • 1
    Looks like the backend argument is also required along with form_class: 'backend': 'registration.backends.default.DefaultBackend' – Abid A Jul 05 '11 at 22:34
5

Here is a practical example using a custom form and backend which sets username == email address, and only prompts the user for an email address at registration. In, for e.g. my_registration.py:

from django.conf import settings
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site

from registration import signals
from registration.forms import RegistrationForm
from registration.models import RegistrationProfile
from registration.backends.default import DefaultBackend

class EmailRegistrationForm(RegistrationForm):
    def __init__(self, *args, **kwargs):
        super(EmailRegistrationForm,self).__init__(*args, **kwargs)
        del self.fields['username']

    def clean(self):
        cleaned_data = super(EmailRegistrationForm,self).clean()
        if 'email' in self.cleaned_data:
            cleaned_data['username'] = self.cleaned_data['username'] = self.cleaned_data['email']
        return cleaned_data


class EmailBackend(DefaultBackend):
    def get_form_class(self, request):
        return EmailRegistrationForm

In my_registration_urls.py:

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

from registration.views import activate
from registration.views import register

urlpatterns = patterns('',
                   url(r'^activate/complete/$',
                       direct_to_template,
                       { 'template': 'registration/activation_complete.html' },
                       name='registration_activation_complete'),
                   # Activation keys get matched by \w+ instead of the more specific
                   # [a-fA-F0-9]{40} because a bad activation key should still get to the view;
                   # that way it can return a sensible "invalid key" message instead of a
                   # confusing 404.
                   url(r'^activate/(?P<activation_key>\w+)/$',
                       activate,
                       { 'backend': 'my_registration.EmailBackend' },
                       name='registration_activate'),
                   url(r'^register/$',
                       register,
                       { 'backend': 'my_registration.EmailBackend' },
                       name='registration_register'),
                   url(r'^register/complete/$',
                       direct_to_template,
                       { 'template': 'registration/registration_complete.html' },
                       name='registration_complete'),
                   url(r'^register/closed/$',
                       direct_to_template,
                       { 'template': 'registration/registration_closed.html' },
                       name='registration_disallowed'),
                   (r'', include('registration.auth_urls')),
                   )

Then in your core urls.py, ensure you include:

url(r'^accounts/', include('my_registration_urls')),
Darb
  • 1,463
  • 11
  • 10
1

You'll need to write a new registration form somewhere in your project. You can inherit off of the existing authentication form if you're just expanding new fields. You'll then want to write a new backend to process the form. Finally you'll need to write your own url and auth_urls and redefine the urls to switch the backend and authentication form in the views by changing the variables that get passed to the view.

It's helpful to break open the source to see how things are working. I base my structure off of the original django-registration code to keep things consistent.

digitaldreamer
  • 52,552
  • 5
  • 33
  • 28
  • This form already exists in the forms.py file. Do I still need to write a new registration form? I literally want to use the other class (referenced in Question) instead of the standard Reg class .. Just not sure how to get that one to be the active form. – Brenden Jun 20 '11 at 17:48
0

As to django 1.11 and django-registration 2.2 there are some updated imports... so if you get "No module named 'registration'" this could be the problem... Replace:

  • from registration.backends.hmac.views import RegistrationView by from django_registration.backends.activation.views import RegistrationView

  • from registration.forms import RegistrationForm by from django_registration.forms import RegistrationForm

  • include('django_registration.backends.hmac.urls') in urls by include('django_registration.backends.activation.urls')

Just to name a few... ;)

Src: https://django-registration.readthedocs.io/en/3.0/custom-user.html

chakmear
  • 101
  • 5