django - 真实 - 缺少必要的潜在论证'自我'

时间:2018-01-29 15:04:49

标签: 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)

,则用户已登录

但问题是,如果我去控制面板,用户不再登录,会话不会保留,可能是因为ACP使用user.代替users.

1 个答案:

答案 0 :(得分:1)

您只需要使用您的班级创建的对象访问该函数。

temp = OwnAuthBackend()
user = temp.authenticate(email=email, password=password)