gpt4 book ai didi

python - Django:无法导入名称 check_password

转载 作者:太空宇宙 更新时间:2023-11-04 06:41:50 25 4
gpt4 key购买 nike

我正在尝试设置自定义用户模型,但在尝试对成员进行身份验证时,我的登录页面出现以下错误

/members/login/导入错误
无法导入名称 check_password

我知道 check_password方法存在,但不确定我缺少什么。尝试导入它时我在做什么?

/members/backends.py

from django.conf import settings
# failing to import check_password here
from django.contrib.auth.models import check_password
from members.models import Member


class EmailAuthBackend(object):
"""Custom authentication backend the allows users to log in using
their email address."""

def authenticate(self, email=None, password=None):
"""Authentication method."""
try:
member = Member.objects.get(email=email)
if member.check_password(password):
return member
except Member.DoesNotExist:
return None

mebers/views.py

from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from django.contrib.auth import login as django_login, authenticate, logout as django_logout

from members.forms.login import LoginForm
from members.forms.register import RegisterForm

def login(request):
"""Login view."""
if request.method != 'POST':
form = LoginForm()

form = LoginForm(data=request.POST)

if form.is_valid():
member = authenticate(email=request.POST.get('email'),
password=request.POST.get('password1'))
if member is not None:
if member.is_active:
django_login(request, member)
return redirect('/')
else:
form = LoginForm()

return render_to_response('members/login.html', {
'form': form
}, context_instance=RequestContext(request))

最佳答案

试试这个:

from django.contrib.auth.hashers import check_password

关于python - Django:无法导入名称 check_password,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38063531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com