小编Ete*_*nal的帖子

django 中的自定义身份验证不起作用

我是 django 新手,我想对用户进行身份验证,因此email我编写了一个自定义身份验证,如文档中所示,但它似乎没有被调用,我不知道我该怎么做?usernamepassword

设置.py

AUTHENTICATION_BACKENDS = ('accounts.backend.AuthBackend',)
Run Code Online (Sandbox Code Playgroud)

视图.py

def login(request):
    if request.method == 'POST':
        username_or_email = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username_or_email, password=password)
        print(user)
        if user is not None:
            return reverse('task:home')
        else:
            messages.error(request, "Username or password is invalid")
            return render(request, 'accounts/login.html')
    else:
         return render(request, 'accounts/login.html')
Run Code Online (Sandbox Code Playgroud)

后端.py

from django.contrib.auth.models import User
from django.db.models import Q


class AuthBackend(object):
    supports_object_permissions = True
    supports_anonymous_user = False
    supports_inactive_user = False

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None …
Run Code Online (Sandbox Code Playgroud)

django django-authentication python-3.x

5
推荐指数
1
解决办法
3953
查看次数

标签 统计

django ×1

django-authentication ×1

python-3.x ×1