Python Django ModelBackend.authenticate如何工作?
我正在调查这件事 我很困惑Python Django ModelBackend.authenticate如何工作?,python,django,authentication,Python,Django,Authentication,我正在调查这件事 我很困惑 其中调用了authenticate() 将用户名和密码传递给authenticate()的是什么 有时,代码可以工作,但我不知道它是如何工作的 更新 我正在阅读一个项目的源代码。我找到了authenticate()的定义,但找不到它的调用位置 grep -r "authenticate" . ./src/myproject/views.py: if request.user.is_authenticated(): ./src/lib/backend.py:
authenticate()
用户名
和密码
传递给authenticate()
的是什么authenticate()
的定义,但找不到它的调用位置
grep -r "authenticate" .
./src/myproject/views.py: if request.user.is_authenticated():
./src/lib/backend.py: def authenticate(self, username = None, password = None, **kwargs):
./src/lib/middleware.py: if not request.user.is_authenticated():
./src/lib/decorators.py: if request.user.is_authenticated():
authenticate()
如果您的项目或应用程序实现了登录表单,那么您或用于身份验证的应用程序的开发人员将调用authenticate()
例如,如果您有一个带有username
和password
字段的登录表单,那么您可以在post()
方法中调用authenticate(username,password)
比如,
if request.method == 'POST':
# Gather the username and password provided by the user.
# This information is obtained from the login form.
username = request.POST['username']
password = request.POST['password']
# Use Django's machinery to attempt to see if the username/password
# combination is valid - a User object is returned if it is.
user = authenticate(username=username, password=password)
# If we have a User object, the details are correct.
# If None (Python's way of representing the absence of a value), no user
# with matching credentials was found.
if user:
# Is the account active? It could have been disabled.
if user.is_active:
# If the account is valid and active, we can log the user in.
# We'll send the user back to the homepage.
login(request, user)
return HttpResponseRedirect('/rango/')
else:
# An inactive account was used - no logging in!
return HttpResponse("Your Rango account is disabled.")
else:
# Bad login details were provided. So we can't log the user in.
print "Invalid login details: {0}, {1}".format(username, password)
return HttpResponse("Invalid login details supplied.")
有关此代码的完整说明,请参阅,或者查看官方django网站上的authenticate()
Er,当您要验证用户身份时,可以调用它,并将用户名和密码传递给它。@DanielRoseman我正在阅读一个项目的源代码。我找到了authenticate()的定义,但找不到它的调用位置。它是由您调用的。@DanielRoseman我仍然感到困惑。您可能正在使用Django提供的登录视图,该视图调用authenticate()
。这就解释了为什么在项目的源代码中找不到它。
if request.method == 'POST':
# Gather the username and password provided by the user.
# This information is obtained from the login form.
username = request.POST['username']
password = request.POST['password']
# Use Django's machinery to attempt to see if the username/password
# combination is valid - a User object is returned if it is.
user = authenticate(username=username, password=password)
# If we have a User object, the details are correct.
# If None (Python's way of representing the absence of a value), no user
# with matching credentials was found.
if user:
# Is the account active? It could have been disabled.
if user.is_active:
# If the account is valid and active, we can log the user in.
# We'll send the user back to the homepage.
login(request, user)
return HttpResponseRedirect('/rango/')
else:
# An inactive account was used - no logging in!
return HttpResponse("Your Rango account is disabled.")
else:
# Bad login details were provided. So we can't log the user in.
print "Invalid login details: {0}, {1}".format(username, password)
return HttpResponse("Invalid login details supplied.")