0
我在Django
中遇到了authenticating
用戶名和密碼這兩種方式。 這是首選方法,爲什麼?在Django中驗證用戶登錄的更好方法?
方式一:
from django.contrib.auth import authenticate, login, logout
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
drinker = authenticate(username=username, password=password)
if drinker is not None:
login(request, drinker)
return HttpResponseRedirect('/profile/')
其他方式:
from django.contrib.auth.backends import ModelBackend
class MyModelBackend(ModelBackend):
def authenticate(self, username=None, password=None):
print "My Logic"
return super(MyModelBackend, self).authenticate(username=username, password=password)