python - django - 验证 - 缺少所需的潜在参数 'self'

标签 python django

我有一个自定义身份验证后端,我正在尝试将用户与它连接起来。

这是后端

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/

相关文章:

python - Python 中的楼层划分背后发生了什么?

python - 为什么 `url` 没有出现在 DRF 响应中

python - 用印度名字训练 Spacy NER

python - 如何使用 vtkOBBTree 和 IntersectWithLine 在 python 中使用 vtk 查找直线和 PolyDataFilter 的交集?

python - 不使用 graphviz/web 可视化决策树

python - 如何在任务中获取 celery 结果模型(使用 django-celery-results)

python - 序列化后如何从查询更新json数据?

python - 从没有 `in` 或 `set` 关键字的有序列表中删除重复值

python - 防止在 Django 中自动转义自定义模板标签

django - Django 中的依赖注入(inject)