login()
在view中使用来登录用户。参数包括一个HttpRequest对象和一个User对象。login() 将用户ID保存到session中(使用Django session framework)。
注意:当用户登陆后对于匿名session的任何数据设置都会保留在session中
首先调用authenticate()
如果你自己写代码,一定要确保首先调用authenticate(),然后再调用login()。 authenticate() 会设置User的一个属性来通知认证后台该用户已经成功认证(具体参见:backends documentation),这个属性信息在后续的login处理中需要。因此,如果你直接调用login就会抛出异常。
logout()
通过 django.contrib.auth.login()登录的用户登出时要在view中使用django.contrib.auth.logout() 。使用HttpRequest对象为参数,无返回值。例如:
注意:logout()不会抛出异常,即使用户并没有登录。
当调用logout()时,当前请求的session数据会彻底清空。如果你需要在用户登出后使用session中的数据,务必在调用django.contrib.auth.logout()后在设置
一.修改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 %}