我正在尝试设置自定义用户模型,但在尝试验证成员时,我的登录页面上出现以下错误
ImportError at /members/login/
cannot import name 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))
答案 0 :(得分:0)
你似乎对功能和方法有点困惑。 check_password
是auth.models.User类的一个方法;你不能自己导入它。而你无论如何都不能使用它;你在你自己的成员班上调用 - 完全独立的 - check_password
方法;没有必要在那里进口任何东西。
答案 1 :(得分:0)
尝试一下:
from django.contrib.auth.hashers import check_password