Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django中为SQLServer数据库创建身份验证系统?_Python_Sql Server_Django_Authentication_Visual Studio Code - Fatal编程技术网

Python 如何在django中为SQLServer数据库创建身份验证系统?

Python 如何在django中为SQLServer数据库创建身份验证系统?,python,sql-server,django,authentication,visual-studio-code,Python,Sql Server,Django,Authentication,Visual Studio Code,我最近在一个django项目中使用SQLServer数据库。我已经用SQLServer连接了数据库,我想为数据库中的表创建一个身份验证系统 我知道django附带了一个内置的身份验证系统,但是没有办法告诉django使用数据库中的特定表进行身份验证,它只是在默认的管理页面中查找用户 django有没有办法在SQLServer数据库中的特定表中查找数据并验证用户输入的信息?您可以通过实现自己的用户模型,然后告诉django如何验证用户身份来实现这一点。您的模型应该如下所示: class Users

我最近在一个django项目中使用SQLServer数据库。我已经用SQLServer连接了数据库,我想为数据库中的表创建一个身份验证系统

我知道django附带了一个内置的身份验证系统,但是没有办法告诉django使用数据库中的特定表进行身份验证,它只是在默认的管理页面中查找用户


django有没有办法在SQLServer数据库中的特定表中查找数据并验证用户输入的信息?

您可以通过实现自己的用户模型,然后告诉django如何验证用户身份来实现这一点。您的模型应该如下所示:

class Users(models.Model):
    id = models.IntegerField(primary_key=True)
    is_active = models.IntegerField(default=1)
    date_joined = models.DateTimeField(default=timezone.now)
    last_login = models.DateTimeField(default=timezone.now)
    username = models.CharField(max_length=30, unique=True)
    password = models.CharField(max_length=30)

    @property
    def is_authenticated(self):
        return True
from django.contrib.auth.backends import ModelBackend
from users.models import Users
from django.contrib.auth.hashers import *
from login.util import *

class PersonalizedLoginBackend(ModelBackend):
    def authenticate(self, request=None, username=None, password=None, **kwars):
        #Here define you login criteria, like encrypting the password and then
        #Checking it matches. This is an example:
        try:
            user = Users.objects.get(username=username)
        except Users.DoesNotExist:
            return None
        if check_password(password, user.password):
            return user
        else:
            return None

    def get_user(self, user_id):
        #This shall return the user given the id
        from django.contrib.auth.models import AnonymousUser
        try:
            user = Users.objects.get(id=user_id)
        except Exception as e:
            user = AnonymousUser()
        return user
您可以添加额外的字段,但django需要这些字段。根据文件中的定义,经过认证的财产应始终为真

下一步是定义您的登录如何进行身份验证。在项目中的任意位置创建一个文件名backends.py,其中声明了两种方法:authenticate和get_user,它应该是这样的:

class Users(models.Model):
    id = models.IntegerField(primary_key=True)
    is_active = models.IntegerField(default=1)
    date_joined = models.DateTimeField(default=timezone.now)
    last_login = models.DateTimeField(default=timezone.now)
    username = models.CharField(max_length=30, unique=True)
    password = models.CharField(max_length=30)

    @property
    def is_authenticated(self):
        return True
from django.contrib.auth.backends import ModelBackend
from users.models import Users
from django.contrib.auth.hashers import *
from login.util import *

class PersonalizedLoginBackend(ModelBackend):
    def authenticate(self, request=None, username=None, password=None, **kwars):
        #Here define you login criteria, like encrypting the password and then
        #Checking it matches. This is an example:
        try:
            user = Users.objects.get(username=username)
        except Users.DoesNotExist:
            return None
        if check_password(password, user.password):
            return user
        else:
            return None

    def get_user(self, user_id):
        #This shall return the user given the id
        from django.contrib.auth.models import AnonymousUser
        try:
            user = Users.objects.get(id=user_id)
        except Exception as e:
            user = AnonymousUser()
        return user
现在,您需要在settings.py上告诉django后端在哪里:

AUTHENTICATION_BACKENDS = (
    # ... your other backends
    'path.to.backends.PersonalizedLoginBackend',
)
从那里,你应该能够像平常一样登录,首先进行身份验证,然后使用do_登录功能

请在此处阅读更多详细信息: