0

视图.py

def loginajax(request):
    form = LoginForm(request.POST)
    #redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '')
    if request.POST:
            #errors = manipulator.get_validation_errors(request.POST)
        if form.is_valid():   
            if request.is_ajax: 
                user = authenticate(username = request.POST['username'],password = request.POST['password'])
                if user is not None:
                    redirect_to = '/home/%s/'%user
                else:
                    redirect_to = '/accounts/activate/'
                return HttpResponse(redirect_to)
            else:
                return HttpResponse("False")
        else:
           return HttpResponse(form.errors)

脚本.js

$('#loginform').submit(function(e){
    $.ajax({
        type:"POST",
        url:"/accounts/login/ajax/",
        data:$('#loginform').serialize(),
        success: function(msg){
            window.location = msg;
        }
    });
    e.preventDefault();             
});

怀疑

我无法理解它何时返回重定向字符串,它说用户未通过身份验证并将我重定向到注册页面,请帮助

4

1 回答 1

1

你忘了(从 django.contrib.auth 导入验证,登录)

login( request, user )

redirect_to = '/home/%s/'%user

authenticate(...) 只是测试用户名和密码是否正确。

于 2012-08-10T20:15:53.127 回答