对django 认证模块的学习

07-05-01) django通过以下中间件提供对认证的支持
'django.contrib.sessions.middleware.SessionMiddleware' 'django.contrib.auth.middleware.AuthenticationMiddleware'

应用模块为
'django.contrib.auth'
'django.contrib.sessions'
'django.contrib.admin'

07-05-02) django登录与注销login/logout
login:


from django.contrib.auth import authenticate, login
def my_view(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)
            # Redirect to a success page.
        else:
            # Return a 'disabled account' error message
    else:
        # Return an 'invalid login' error message.

logout:


from django.contrib.auth import logout
def logout_view(request):
    logout(request)
    # Redirect to a success page.

如果是非登录用户使用logout注销时,此函数不会提示任何错误或引发异常

调用logout时,当前登录中的session会话数据(sessionid)会自动清除

07-05-03) 详细了解django.contrib.auth下的login/logout的API

你可能感兴趣的:(对django 认证模块的学习)