post与后端django数据交互

参考办法:

https://blog.csdn.net/qq_41000891/article/details/82902294

 

【前端】

loginsubmit(){
   console.log(this.UserName+this.UserPwd);
   axios.post("http://localhost:8000/baseapi/login",{
       userid:"zhonghangAlex",
       password:"woaini123"
   }).then((response)=>{
       let res = response.data;
       console.log(res);

       this.$router.push('/user/userindex')
   });
 
}

【后端】

def login(request):
    if request.method == 'GET':
        return HttpResponse('{"status":"0","message":"请使用post请求","result":"null"}')
    elif request.method == 'POST':
        req = json.loads(request.body)

        req_userid = req['userid']
        req_password = req['password']
        if req_userid and req_password:
            try:
                login_obj = models.T_user_info.objects.filter(userid = req_userid,password = req_password).first()
            except:
                return HttpResponse('{"status":"0","message":"用户名或密码错误","result":"null"}')
            else:
                request.session['userid'] = login_obj.userid
                request.session['power'] = login_obj.power
                return HttpResponse('{"status":"1","message":"登陆成功","result":{"power":"'+str(login_obj.power)+'"}}')
        else:
            HttpResponse('{"status":"0","message":"输入有误,请重新输入","result":"null"}')

你可能感兴趣的:(vue,Python,Django)