3

目前我正在尝试实现登录验证系统。我正在使用 ajax,以便用户可以在不被重定向到另一个页面的情况下获得响应。我的 ajax 函数发送用户输入的电子邮件和密码,并在回调函数中获取消息,可以分为三种类型:电子邮件、密码或实际的 HttpResponse 对象。但我不知道如何使用 ajax 和 jquery 呈现给定的 http 响应对象。location.href 是一个选项吗?我正在粘贴下面的代码。

在 JavaScript 中:

function loginSubmit(email, password) {
    var d= "email=" + email + "&password=" + password;
    $.ajax({
        url: "/login",
        type: "POST",
        dataType: "text",
        data: d,
        success: function(m) {
            if (m == "email") {
                $("#emailMessage").html("There is no account associated with this email address.");
                $("#emailError").show();
                $("#emailError").fadeOut(5000, function() {});
            } else if (m == "password") {
                $("#emailMessage").html("There is no account associated with this email address.");
                $("#emailError").show();
                $("#emailError").fadeOut(5000, function() {});
            } else {

            }
        }
    });
}

视图功能:

def login(request):
    json = request.POST
    e = json['email']
    p = json['password']

    u = User.objects.filter(email=e)

    if (len(u)):
        up = User.objects.filter(email=e, password=p)
        if (len(up)):
            return render_to_response('profile.html', context_instance=RequestContext(request))
        else:

            data = "password"
            c = RequestContext(request, {'result':data})
            t = Template("{{result}}")
            datatype=u"application/javascript"
            return HttpResponse(t.render(c), datatype)
    else:
        data = "email"
        c = RequestContext(request, {'result':data})
        t = Template("{{result}}")
        datatype=u"application/javascript"
        return HttpResponse(t.render(c), datatype)

ps 目前我正在使用虚拟模板和 HttpResponse 将数据发送到 ajax 成功回调函数。有没有更有效的方法来完成这个(发回 json 数据)?我会等待你们的回复!

4

1 回答 1

4
from django.contrib.auth import authenticate, login as auth_login

def login(request):

    # Use authentication framework to check user's credentials
    # http://djangosnippets.org/snippets/1001/ for auth backend
    user = authenticate(
               email    = request.POST['email'],
               password = request.POST['password'], )

    if user is not None:
        # Use Auth framework to login user
        auth_login(request, user)
        return render_to_response('profile.html',
                 context_instance=RequestContext(request))

    else:
        # Return Access Denied
        # Never return bad email/bad password. This is information leakage
        # and helps hackers determine who uses your platform and their emails.
        return HttpResponse("Failed: Bad username or password", status=403)


function loginSubmit(email, password) {
    $.ajax({
        url: "/login",
        type: "POST",
        data: {email:email, password:password},
        success: function(data) {
            var returned_html = $(data);
            $("#target_profile_area").clear().append(returned_html);
        },
        error: function(jqXHR) {
            if (jqXHR.statusCode == 403) {
                $("#loginMessage").text("Your login details are incorrect");
            } else {
                $("#loginMessage").text("Error Contacting Server");
            }
            $("#loginError").show();
            $("#loginError").fadeOut(5000, function() {});
        }
    });
}
于 2012-07-20T07:01:10.960 回答