本文介绍了如何在set_cookie函数django中添加samesite = None?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 set_cookie函数

这是我调用 set_cookie函数

redirect = HttpResponseRedirect( '/m/' )
redirect.set_cookie( 'access_token', access_token, max_age=60 * 60 )

这是我设置Cookie的功能

This is the function where I set the cookie

def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
               domain=None, secure=False, httponly=False):
    self.cookies[key] = value
    if expires is not None:
        if isinstance(expires, datetime.datetime):
            if timezone.is_aware(expires):
                expires = timezone.make_naive(expires, timezone.utc)
            delta = expires - expires.utcnow()
            delta = delta + datetime.timedelta(seconds=1)
            expires = None
            max_age = max(0, delta.days * 86400 + delta.seconds)
        else:
            self.cookies[key]['expires'] = expires
    else:
        self.cookies[key]['expires'] = ''
    if max_age is not None:
        self.cookies[key]['max-age'] = max_age
        # IE requires expires, so set it if hasn't been already.
        if not expires:
            self.cookies[key]['expires'] = cookie_date(time.time() +
                                                       max_age)
    if path is not None:
        self.cookies[key]['path'] = path
    if domain is not None:
        self.cookies[key]['domain'] = domain
    if secure:
        self.cookies[key]['secure'] = True
    if httponly:
        self.cookies[key]['httponly'] = True

推荐答案

如果您使用的是django2.x或更旧的版本,则可以使用此库来更改标志:"> https://pypi.org/project/django-cookies-samesite/

You can use this library to change the flag if you're using django2.x or older: https://pypi.org/project/django-cookies-samesite/

如果您使用的是django3.x,它应该是内置的

If you're using django3.x, it should be built-in

这篇关于如何在set_cookie函数django中添加samesite = None?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 14:02