Django cookie设置

时间:2017-01-27 09:24:07

标签: django session-cookies django-sessions

我在我的项目中使用Django-ldap身份验证。一旦用户通过身份验证,我需要设置一个cookie并作为对服务器的响应返回。

def post(self,request):
        userData = json.loads(request.body)
        username = userData.get('username') 
        password = userData.get('password')

        oLdap = LDAPBackend()


        if username == "admin" and password == "admin":
            User_Grps = "AdminLogin"
        else:
            try:
                User = oLdap.authenticate(username=username,password=password) 
                if User is not None:
                    User_Grps = User.ldap_user.group_dns
                else:
                    User_Grps = "Check your username and password"

            except ldap.LDAPError:
                   User_Grps = "Error"

        return HttpResponse(User_Grps)

如何将cookie添加到响应中并使用User_Grps将其发送到客户端

1 个答案:

答案 0 :(得分:0)

response = HttpResponse(User_Grps)
response.set_cookie(key, value)
return response

就是这样。