Login
Authentiacting users
authenticate(request=None, **credentials)
Use authenticate() to verify a set of credentials. It takes credentials as keyword arguments, username and password for the default case, checks them against each authentication backend, and returns a User object if the credentials are valid for a backend. If the credentials aren’t valid for any backend or if a backend raises PermissionDenied, it returns None. For example:
from django.contrib.auth. import authenticate
username=request.POST.get('username')
password=request.POST.get('password')
user = authenticate(username, password)
if user:
login(request, user)
do something
else:
do something
Login
django.contrib.auth.login
login(request, user, backend=None)
Note that any data during the anonymous session is retained(匿名会话在会话中保留) in the session after a user logs in.
一般
user = authenticate(username, password) login(request, user)
Logout
django.contrib.auth.logout
logout(request)
To log out a user who has been logged in via django.contrib.auth.login(), use django.contrib.auth.logout() within your view. It takes an HttpRequest object and has no return value.