简体   繁体   English

将 LoginBackEnd 模块导入 settings.py 时出错

[英]Getting error when I import LoginBackEnd module into settings.py

I am using just phone number alone as the only field for login.我只使用电话号码作为唯一的登录字段。 So I am trying to write my custom backend authentication.所以我正在尝试编写我的自定义后端身份验证。 On trying to import the LoginBackend module, I am getting the error below.在尝试导入 LoginBackend 模块时,出现以下错误。

ERROR WHEN I CONFIGURE THE AUTH BACKENDS:配置身份验证后端时出错:

 File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\conf\__init__.py", line 161, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
@csrf_exempt
@api_view(["POST"])
@permission_classes((AllowAny,))
def logins(request):
    phone_number = request.data.get("phone_number")
    if phone_number is None:
        return Response({'error': 'Please provide your phone number'},
                        status=HTTP_400_BAD_REQUEST)
    user = authenticate(phone_number=phone_number)
    if not user:
        return Response({'error': 'Invalid Credentials'},
                        status=HTTP_404_NOT_FOUND)
    token, _ = Token.objects.get_or_create(user=user)
    return Response({'token': token.key},
                    status=HTTP_200_OK)

backends.py后端.py

from django.contrib.auth.backends import ModelBackend
from .models import User

class LoginBackend(ModelBackend):
    def authenticate(self, request, **kwargs):
        phone_number= kwargs['phone_number']
        user = User.objects.get(phone_number=phone_number)
        if user:
            return user
        else:
            return None

settings.py设置.py

from .backends import LoginBackend
from django.contrib.auth.backends import ModelBackend
AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend',
    'findr.backends.LoginBackend']

使这些东西正常工作所需要做的就是撤消 settings.py 中的导入语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试导入自定义后端时,Settings.py引发错误 - Settings.py throws error when I try to import my custom backend 我不断收到错误“ModuleNotFoundError:我的 settings.py 文件中没有名为‘environ’的模块”。 我已经在我的 python shell 中安装了依赖项 - I keep getting the error "ModuleNotFoundError: No module named 'environ' in my settings.py file". I have installed the dependency in my python shell Django部署:ImportError:无法导入设置“ settings.py”(在sys.path上吗?):没有名为settings.py的模块 - Django Deployment: ImportError: Could not import settings 'settings.py' (Is it on sys.path?): No module named settings.py Django settings.py错误:不支持按文件名导入 - Django settings.py Error: Import by filename is not supported 我应该在 settings.py 中写什么来导入我的模板 - what should i write in the settings.py to import my Template 无法将 .py 文件导入 settings.py - Cannot import .py file into settings.py ImportError:无法导入设置“ settings.py”(在sys.path上吗?):没有名为py的模块 - ImportError: Could not import settings 'settings.py' (Is it on sys.path?): No module named py 在Django的settings.py中导入模型 - Import model in Django's settings.py 尽管我在 settings.py 文件中提供了 CORS_ORIGIN_ALLOW_ALL = True - getting strict-origin-when-cross-origin although i provided CORS_ORIGIN_ALLOW_ALL = True in settings.py file django settings.py值错误 - django settings.py value error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM