我有一个自定义身份验证后端,我正在尝试将用户与它连接起来。
这是后端
import logging
from .models import Users
class OwnAuthBackend(object):
def authenticate(self, email, password):
try:
user = Users.objects.get(email=email)
if user.check_password(password):
return user
else:
return None
except Users.DoesNotExist:
logging.getLogger("error_logger").error("user with login %s does not exists " % login)
return None
except Exception as e:
logging.getLogger("error_logger").error(repr(e))
return None
def get_user(self, user_id):
try:
user = Users.objects.get(sys_id=user_id)
if user.is_active:
return user
return None
except Users.DoesNotExist:
logging.getLogger("error_logger").error("user with %(user_id)d not found")
return None
这是在 view.py
中
email = form.cleaned_data['email_field']
password = form.cleaned_data['password']
user = OwnAuthBackend.authenticate(email=email, password=password)
if user is not None:
messages.success(request, 'You logged in successfully!')
return redirect('index')
else:
messages.warning(request, 'Credentials were wrong. Failed to log in!')
return redirect('index')
message.error(request, 'Something went wrong while logging you in..')
return redirect('index')
导入:
from main.backends import OwnAuthBackend
标题有错误
我真的不知道什么self
我必须做,尝试放置 request
在那里,但那不起作用。
根据:users.is_authenticated
- 自定义用户模型,如果我使用user = OwnAuthBackend.authenticate(request, email=email, password=password)
则用户已登录
但问题是,如果我进入控制面板,用户不再登录, session 不会保留,可能是因为 ACP 使用 user.
而不是users.
最佳答案
您只需要使用您的类创建的对象来访问该函数。
temp = OwnAuthBackend()
user = temp.authenticate(email=email, password=password)
关于python - django - 验证 - 缺少所需的潜在参数 'self',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48504247/