一.添加django_test/forms.py的内容如下
from django import forms from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm class MyRegistrationForm(UserCreationForm): email=forms.EmailField(required=True) class Meta: model=User fields=('username','email','password1','password2') def save(self,commit=True): user=super(UserCreationForm,self).save(commit=False) user.email=self.cleaned_data['email'] if commit: user.save() return user
二.修改django_test/views.py的内容如下
from django.shortcuts import render_to_response from django.http import HttpResponseRedirect,HttpResponse from django.contrib import auth from django.core.context_processors import csrf from forms import MyRegistrationForm from datetime import datetime import urllib,urllib2,cookielib def login(request): c= {} c.update(csrf(request)) return render_to_response('login.html',c) def auth_view(request): username=request.POST.get('login','') password=request.POST.get('password','') user=auth.authenticate(username=username,password=password) if user is not None: auth.login(request,user) return HttpResponseRedirect('/accounts/loggedin') else: return HttpResponseRedirect('/accounts/invalid') def loggedin(request): return render_to_response('loggedin.html', {'full_name':request.user.username}) def invalid_login(request): return render_to_response('invalid_login.html') def logout(request): auth.logout(request) return render_to_response('logout.html') def register_user(request): if request.method =='POST': form = MyRegistrationForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirct('/accounts/register_success') args={} args.update(csrf(request)) args['form']=MyRegistrationForm() return render_to_response('register.html',args) def register_success(request): return render_to_response('register_success.html')