3

現在、ログイン検証システムを実装しようとしています。ユーザーが別のページにリダイレクトされることなく応答を受け取ることができるように、私はajaxを使用しています。私のajax関数は、ユーザーが入力した電子メールとパスワードを送信し、コールバック関数でメッセージを取得します。これは、電子メール、パスワード、または実際のHttpResponseオブジェクトの3つのタイプになります。しかし、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 に答える