0

我在此视图中对用户进行身份验证:

def note_edit_auth(request):
        if request.method == 'POST':
                form = EditAuthForm(request.POST)
                if form.is_valid():
                        email = form.cleaned_data['email']
                        password = form.cleaned_data['password']
                        user = auth.authenticate(username = email, password = password)

                        if user and user.is_authenticated():
                                request.session['email'] = email
                                request.session['password'] = password
                                request.session['user'] = user
                                return HttpResponseRedirect('/note_edit')
                        else:
                                return HttpResponse('User or password is either incorrect or does not exist.')

它有效。user确实是经过认证的。所以然后我尝试request.user从上面的视图重定向到这样的另一个视图访问:

def note_edit(request):
        if request.method == 'POST' and request.user.is_authenticated():
                form_edit = EditForm(request.POST)
                user = request.session['email']
                auth.logout(request)
                return HttpResponse('Done!')
        elif request.method == 'GET' and request.user.is_authenticated():
                form_edit = EditForm(irrelevantfoofoo)
        else:
                return HttpResponse('User not authenticated.')
        return render_to_response('editnote.htm', {'form': form_edit}, context_instance = RequestContext(request))

突然我得到用户未通过身份验证的响应。当我尝试打印request.user到控制台时,它显示为 AnonymousUser。我骗你,这昨晚工作得很好。今天我醒来,做了一些改变,它开始行为不端。我还原了所有更改,但它仍然行为不端。

为什么会发生这种情况,我该如何解决?

4

1 回答 1

0

auth.authenticate()只检查提供的用户凭据是否有效。要实际登录用户,您必须使用login().

于 2013-01-31T19:10:41.003 回答