Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Django设置cookies?_Python_Django_Session_Cookies - Fatal编程技术网

Python 如何使用Django设置cookies?

Python 如何使用Django设置cookies?,python,django,session,cookies,Python,Django,Session,Cookies,我想使用request.session方法添加cookie,但它不起作用 我如何使用它 分歧 您可能知道,cookies和session之间存在着巨大的差异。 Cookies在客户端存储数据。 会话使用cookie作为密钥,并将其与存储在服务器端的数据相关联 会话更好 通常最好使用会话而不是cookies,因为数据对客户端是隐藏的,并且您可以轻松地设置数据何时过期为无效 安全原因 在安全方面,如果它都是围绕cookie构建的,恶意用户可能会更改其cookie数据,并向您的网站发送错误请求 但是

我想使用
request.session
方法添加cookie,但它不起作用

我如何使用它

  • 分歧
  • 您可能知道,cookies和session之间存在着巨大的差异。 Cookies在客户端存储数据。 会话使用cookie作为密钥,并将其与存储在服务器端的数据相关联

  • 会话更好
  • 通常最好使用会话而不是cookies,因为数据对客户端是隐藏的,并且您可以轻松地设置数据何时过期为无效

  • 安全原因
  • 在安全方面,如果它都是围绕cookie构建的,恶意用户可能会更改其cookie数据,并向您的网站发送错误请求


    但是如果您真的想使用cookie,Django现在可以处理请求和响应对象上的直接cookie操作方法

    您可以按以下方式进行操作:

    视图.py

    def signin(request):
        if request.method == "POST":
            form = LoginForm(request.POST)
            email_input = str(request.POST['email'])
            password_input = str(request.POST['password'])
            user_Qset = Profile.objects.filter(email = email_input)
    
            if user_Qset is not None:
                password_saved = str(user_Qset.values('password')[0]['password'])
                if password_input == password_saved:
                    request.session['name'] = str(user_Qset.values('name')[0]   ['name'])
                    request.session['email'] = str(user_Qset.values('email')[0]['email'])
                    request.session['password'] = str(user_Qset.values('password')[0]['password'])
                    return HttpResponse('login success.')
                else:
                    return HttpResponse('locin failed, wrong password')
            else:
                return HttpResponse('login failed, wrong email address')
        else:
            form = LoginForm()
            return render(request, 'registration/login.html', {'form': form})
    
    来源


    可能重复Try
    响应。设置cookie
    您使用的是会话,而不是cookie。但是为什么你需要特别使用cookies呢?这个课程通常比较好。我是Django的初学者。所以,我练习了使用cookies和session。现在我知道如何设置和获取cookies,现在我正在搜索如何使用会话
    def signin(request):
        response = HttpResponse('login success.')
        if request.method == "POST":
            form = LoginForm(request.POST)
            email_input = str(request.POST['email'])
            password_input = str(request.POST['password'])
            user_Qset = Profile.objects.filter(email = email_input)
    
            if user_Qset is not None:
                password_saved = str(user_Qset.values('password')[0]['password'])
                if password_input == password_saved:
                    response.set_cookie('name', str(user_Qset.values('name')[0]   ['name']))
                    response.set_cookie('email', str(user_Qset.values('email')[0]['email']))
                    response.set_cookie('password', str(user_Qset.values('password')[0]['password']))
                    return response
                else:
                    return HttpResponse('locin failed, wrong password')
            else:
                return HttpResponse('login failed, wrong email address')
        else:
            form = LoginForm()
            return render(request, 'registration/login.html', {'form': form})