Django使用Ajax登录? [英] Django Login with Ajax?

查看:274
本文介绍了Django使用Ajax登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery函数发布带有ajax的登录表单.在ajax成功中:无论是否成功登录,我都希望以不同的方式处理响应.

I'm working on a jquery function to post a login-form with ajax. In the ajax success: I want to handle the response differently if it's a successful login or not.

所以在Django中,我想知道是否可以在现有的登录视图上添加一些成功/错误变量,以将其与返回的页面一起发送回jquery函数.我的意思是保持默认视图不变,但还要添加一个额外的状态"变量.

So in Django I wonder if i can build on the existing login-view to add some success/error variable to send back to the jquery function together with the returned page. I mean keep the view as it works by default but also add an extra "status" variable.

这是一个很好的例子!

推荐答案

您需要设置一个jquery Ajax调用,以将其发布到django中的Login视图中.

You need to setup an jquery Ajax call to post to your Login view in django.

在视图中,您需要执行以下操作...

In the view you need to do something like this...

import json
from django.http import HttpResponse

def login(request):                                                                                                                         
    if request.method == 'POST':                                                                                                                                                                                                           
        login_form = AuthenticationForm(request, request.POST)
        response_data = {}                                                                              
        if login_form.is_valid():                                                                                                           
            response_data['result'] = 'Success!'
            response_data['message'] = 'You"re logged in' 
        else:
            response_data['result'] = 'failed'
            response_data['message'] = 'You messed up'   

        return HttpResponse(json.dumps(response_data), content_type="application/json")  

我还没有测试过,但是你的ajax调用应该看起来像这样.

I have not tested this, but you're ajax call should look something like this.

<script> 
$.ajax({                                                                                                                           
    type:"POST",                                                                                                                    
    url: 'http://www.yousite.com/yourview/login/',                                                                                                   
    data: $('#login_form').serialize(),                                                                                  
    success: function(response){ 
        // do something with response
        response['result']; // equals 'Success or failed';
        response['message'] // equals 'you"re logged in or You messed up';                                                                                                     
     }                                                                                                                             
});
</script>  

这篇关于Django使用Ajax登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆