一.修改settings.py的APPS的内容如下

二.修改django_test/urls.py的内容如下
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
(r'^articles/',include('article.urls')),
url(r'^hello/$','article.views.hello'),
url(r'^hello_template_simple/$','article.views.hello_template_simple'),
# url(r'^$', 'django_test.views.home', name='home'),
# url(r'^django_test/', include('django_test.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# user auth urls
url(r'^accounts/login/$','django_test.views.login'),
url(r'^accounts/auth/$','django_test.views.auth_view'),
url(r'^accounts/logout/$','django_test.views.logout'),
url(r'^accounts/loggedin/$','django_test.views.loggedin'),
url(r'^accounts/invalid/$','django_test.views.invalid_login'),
url(r'^accounts/register/$','django_test.views.register_user'),
url(r'^accounts/register_success/$','django_test.views.register_success'),
)
三.修改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 django.contrib.auth.forms import UserCreationForm
from datetime import datetime
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 = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirct('/accounts/register_success')
args={}
args.update(csrf(request))
args['form']=UserCreationForm()
return render_to_response('register.html',args)
def register_success(request):
return render_to_response('register_success.html')
四.修改django_test/templates/login.html的内容如下
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p class="error">Sorry. that's not a valid username or password </p>
{% endif %}
<form action="/accounts/auth/" method="post">{% csrf_token %}
<label for="username">User name:</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password:</label>
<input type="password" name="password" value="" id="password">
<input type="submit" value="login">
</form>
{% endblock %}
五.修改django_test/templates/loggedin.html的内容如下{% extends "base.html" %}
{% block content %}
<h2>Hi {{full_name}} you are now Logged in!</h2>
<p>Click <a href="/accounts/logout/">here</a> to logout. </p>
{% endblock %}
六.修改django_test/templates/register.html的内容如下
{% extends "base.html" %}
{% block content %}
<h2>Register</h2>
<form action="/accounts/register/" method="post">
{{form}}
<input type="submit" value="Register" />
</form>
{% endblock %}
六.修改django_test/templates/invalid.html的内容如下
{% extends "base.html" %}
{% block content %}
<h2>Your login details are invalid!</h2>
<p>Click <a href="/accounts/login/">here</a>to login again. </p>
{% endblock %}
七.修改django_test/templates/register_success.html的内容如下{% extends "base.html" %}
{% block content %}
<h2>You have registered!</h2>
<p>Click <a href="/accounts/login/">here</a> to login again </p>
{% endblock %}