cookies_ajax

views
def test_user(request):
    print('start')
    if request.method=='POST':
        print('goon_test_user')
        user = request.POST.get('user')
        passwd = request.POST.get('passwd')
        print(user,passwd)
        print('#####')
        if models.UserToPasswd.objects.filter(user=user,passwd=passwd).first():
            print('1111111')
            dic = {'user':user,'status': '1', 'msg': '用户名或密码错误'}
            dic_dump=json.dumps(dic)
            ###
            obj = HttpResponse(dic_dump)
            obj.set_cookie('is_login',True,max_age=100)
            obj.set_cookie('user',user)
            ###
            return obj

        else:
            print('000000000')
            dic = {'user':user,'status':'0','msg':'用户名或密码错误'}
            dic_dump = json.dumps(dic)
            return HttpResponse(dic_dump)

    else:
        return render(request,'test_user.html')

def test2_user(request):
    if request.method=='POST':
        user = request.POST.get('user')
        passwd = request.POST.get('passwd')
        models.UserToPasswd.objects.create(user=user,passwd=passwd)
        # token =request.POST
        return  render(request,'time_go.html',locals())
        # return render(request,'time_go.html',locals())
    return render(request,'sign_in.html')

def success(request,user):
    print('xxxxxxxx')
    print(request.COOKIES)
    if not request.COOKIES.get('is_login'):
        return redirect("/test_user/")
    user= request.COOKIES.get('user')
    # return HttpResponse('Hello sir %s'%user)
    return render(request,'index.html',locals())
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>auth</title>
    <script src="/static/jquery-3.3.1.js"></script>
{#    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js "></script>#}
{#    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">#}
</head>
<style>
    .hide{
        display: none;
    }
</style>
<body>

{#test#}
{#<input type="text" id="in1"> + <input type="text" id="in2"> = <input type="text" id="in3"> <button class="sub">submit</button>#}
{#<hr>#}

<form action="" method="">
    {% csrf_token %}
    <h2>请输入登陆信息</h2>
    <p>用户名:<input type="text"  class="u_p" id="user"></p>
    <p>密码: <input type="password"  class="u_p" id="passwd"></p>
{#    <button class="auth">登陆</button>    <span class="err hide"></span>#}
    <input class="auth" type="button" value="登陆">    <span class="err hide"></span>
</form>

<script>
    $(".auth").click(function () {

        $.ajax({
            url:"/test_user/",
            type:'post',
            data:{'user':$('#user').val(),         {#发送数据到上方的url中,type可以选择,在视图的request中可以将数值取出来,用get字典中的name即‘in1’#}
                    'passwd':$('#passwd').val(),
                    'csrfmiddlewaretoken':$('[name=csrfmiddlewaretoken]').val(),  {#这个是post请求时候用的验证,相当于表单的验证.#}
                    },

            success:function (data) {    {#这里的data是服务器返回来的数据,即return HttpResponse(str(in3))中HttpResponse中的值#}
                var data =JSON.parse(data)
                console.log(data)
                console.log("---------")

                var user = data['user']
                console.log(user)
                var status = data['status']
                var msg= data['msg']
                if (status=='1'){
                     temp="/success/ppp";
                     temp = temp.replace('ppp',user)
                    location.href=temp
                    $('.err').addClass('hide')
                } else {
                    $('.err').text(msg)
                    $('.err').removeClass('hide')

                }

{##}
            },
{#            error:function (data) {#}
{#                var data#}
{#                #}
{#                #}
{#            }            #}

        })

    })

{#    $("#auth").onclick(function () {#}
{#        $.ajax(#}
{#            {url:'/ajax_auth/',#}
{#                type:'post',#}
{#                data:{ 'user':$('#user').val(),#}
{#                        'passwd':$("#passwd").val(),#}
{#                        'csrfmiddlewaretoken':$("[name=csrfmiddlewaretoken]").val()#}
{#                },#}
{#                success:function () {#}
{##}
{##}
{##}
{#                }#}
{##}
{#            }#}
{#        )#}
{##}
{#    })#}
{#   绑定焦点事件 #}
{#    $(".u_p").onfocus(function () {#}
{#        $.ajax(#}
{#            {url:"/test_user/",#}
{#                type:'post',#}
{#                data:{ 'user':$('.u_p').val(),#}
{#                        'csrfmiddlewaretoken':$('[name=csrfmiddlewaretoken]').val(),#}
{#                },#}
{#            success:function (data) {#}
{#                if (data.get('user')==0){#}
{##}
{#                }#}
{##}
{#            }#}
{#            }#}
{##}
{#        )#}
{##}
{#    })#}
{##}


</script>
</body>
</html>

 




xxx

猜你喜欢

转载自www.cnblogs.com/taozizainali/p/8960695.html