1

我有一个login带有自定义功能的authenticate功能。

def authenticate(username='username', password='password'):
    try:
        return GalaxyUser.objects.get(username=username, password=password)
    except GalaxyUser.DoesNotExist:
        return None

def login_backend(request):
    if request.method == 'POST':
        username = request.POST['username']
        username1 = ''.join(username)
        password = request.POST['password']
        password = ''.join(password)
        password1 = hashlib.sha1(password).hexdigest()
        user = authenticate(username=username1, password=password1)
        login(request, user)
        if user is None:
            return HttpResponseRedirect('/unsuccessful/')
        else:
            return HttpResponseRedirect('/successful/')
    else:
        return render_to_response('login_backend.html', context_instance=RequestContext(request))

我没有被重定向到成功或不成功。但是,我收到此错误:

Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/zurelsoft/workspace/genelaytics/fileupload/backend.py" in login_backend
  21.         login(request, user)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/__init__.py" in login
  73.     request.session[BACKEND_SESSION_KEY] = user.backend

Exception Type: AttributeError at /login_backend/
Exception Value: 'GalaxyUser' object has no attribute 'backend'

我究竟做错了什么?GalaxyUser是我的模型名称,backend是我的视图代码所在的位置。

4

1 回答 1

0

因为您的身份验证函数是一个独立的函数,而不是实现文档中指定的身份验证后端 API 的类。

于 2013-01-14T10:53:53.703 回答