Friday, March 23, 2012

Chapter 12: Sessions, users, and registration

Chapter 12: Sessions, users, and registration: "Handling registration1
We can use these low-level tools to create views that allow users to sign up. Nearly every developer wants to implement registration differently, so Django leaves writing a registration view up to you; luckily, it’s pretty easy.

At its simplest, we could provide a small view that prompts for the required user information and creates those users. Django provides a built-in form you can use for this purpose, which we’ll use in this example:1

from django import oldforms as forms
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib.auth.forms import UserCreationForm

def register(request):
form = UserCreationForm()

if request.method == 'POST':
data = request.POST.copy()
errors = form.get_validation_errors(data)
if not errors:
new_user = form.save()
return HttpResponseRedirect("/accounts/created/")
else:
data, errors = {}, {}

return render_to_response("registration/register.html", {
'form' : forms.FormWrapper(form, data, errors)
})
"

'via Blog this'