Python 无密码的身份验证Django
正如标题所述,我正在尝试验证一个没有密码的用户。我已经用过这个方法:解决我的应用程序(在Django 2.0上)的问题,但我想在另一个应用程序中做同样的事情,但它在Django 2.1上。当我执行相同的实现时,我的自定义身份验证函数从未被调用。因此它不起作用 auth_backend.py中的当前设置:Python 无密码的身份验证Django,python,django,authentication,django-authentication,Python,Django,Authentication,Django Authentication,正如标题所述,我正在尝试验证一个没有密码的用户。我已经用过这个方法:解决我的应用程序(在Django 2.0上)的问题,但我想在另一个应用程序中做同样的事情,但它在Django 2.1上。当我执行相同的实现时,我的自定义身份验证函数从未被调用。因此它不起作用 auth_backend.py中的当前设置: from django.contrib.auth.backends import ModelBackend from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
class PasswordlessAuthBackend(ModelBackend):
"""Log in to Django without providing a password.
"""
def authenticate(self, username=None):
try:
return User.objects.get(username=username)
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
在settings.py中设置:
AUTHENTICATION_BACKENDS = [
# auth_backend.py implementing Class PasswordlessAuthBackend inside yourapp folder
'yourapp.auth_backend.PasswordlessAuthBackend',
# Default authentication of Django
'django.contrib.auth.backends.ModelBackend',
]
但当我尝试在我的观点
user = authenticate(username=user.username)
它永远不会影响我的自定义身份验证方法。感谢您的任何帮助 您在settings.py中的身份验证后端路径无效
yourapp.auth_backend.YourAuth
应该是
yourapp.auth_backend.PasswordlessAuthBackend
您可以尝试避免使用默认后端吗 改变
AUTHENTICATION_BACKENDS = [
# auth_backend.py implementing Class PasswordlessAuthBackend inside yourapp folder
'yourapp.auth_backend.PasswordlessAuthBackend',
# Default authentication of Django
'django.contrib.auth.backends.ModelBackend',
]
到
所以我解决了自己的问题,这要归功于这里的文档: 我所要做的就是从auth_backend.py中的authetnicate函数
def authenticate(self, username=None):
到
在文档中,它说您还可以将类delcaration更改为不包含ModelBackend,但这两种方法都有效。这是mb。我已经把它正确地放在我这边了。只是把复制和粘贴搞砸了我实际上解决了我的问题。我所要做的就是将请求作为参数添加到我的authenticate函数中
def authenticate(self, username=None):
def authenticate(self, request, username=None):