RSS
The Testing Goat

Obey the Testing Goat!

TDD for the Web, with Python, Selenium, Django, JavaScript and pals...

Using the built-in views and forms for new user registration in Django

Sun 09 June 2013
By Harry

Have been digging into the built-in forms and views from django.contrib.auth. I always knew you could get generic views for login, logout, even password reset, but I didn't know you could actually handle new user creation as well!

There's a form in django.contrib.auth.forms, and a class-based view for creating new objects called CreateView, and I believe this is all you need for a working registration form/view:

from django.views.generic.edit import CreateView
from django.contrib.auth.forms import UserCreationForm

urlpatterns = patterns('',
    url('^register/', CreateView.as_view(
            template_name='register.html',
            form_class=UserCreationForm,
            success_url='/'
    )),
    url('^accounts/', include('django.contrib.auth.urls')),

    # rest of your URLs as normal
)

Then, in register.html (which you do have to create yourself), you get a {{ form }} you can use, including a username, password and password confirmation, and it handles validation errors and EVERYTHING.

Obviously it doesn't include registering (and validating) an email address for the user, but still, I'm pretty excited. Did everyone else already know about this?

Comments

comments powered by Disqus
Read the book

The book is available both for free and for money. It's all about TDD and Web programming. Read it here!

Reviews & Testimonials

"Hands down the best teaching book I've ever read""Even the first 4 chapters were worth the money""Oh my gosh! This book is outstanding""The testing goat is my new friend"Read more...

Resources

A selection of links and videos about TDD, not necessarily all mine, eg this tutorial at PyCon 2013, how to motivate coworkers to write unit tests, thoughts on Django's test tools, London-style TDD and more.

Old TDD / Django Tutorial

This is my old TDD tutorial, which follows along with the official Django tutorial, but with full TDD. It badly needs updating. Read the book instead!

Save the Testing Goat Campaign

The campaign page, preserved for history, which led to the glorious presence of the Testing Goat on the front of the book.