Django 在 iframe 中不保留会话

Django not retaining session while in iframe

我正在为 Bitrix24 开发应用程序,但我遇到了会话问题。

@method_decorator(csrf_exempt, name='dispatch')
class IndexView(View):
    r'''Index View.'''

    def get(self, request):
        r'''Manages Bitrix24 authentication redirect.'''

        return render(request, 'frontend/index.html')

    # POST working as GET because Bitrix24 does not GET the homepage.
    def post(self, request):
        r'''Renders the home page and stores Bitrix24 domain in session.'''

        request.session['bitrix24_domain'] = request.GET.get('DOMAIN')
        return render(request, 'frontend/index.html')

一旦用户转到另一个视图,会话就完全消失了。它也发生在本地,如果我在会话中设置一个键并尝试从另一个页面检索它,所有数据也会丢失。

我目前正在使用 Django 的默认设置并使用 rest-framework。

有人能帮忙吗?

编辑

我尝试使用 self.request.session 并且它有效,但对我来说没有意义。谁能解释一下?

编辑

我注意到,例如,当我的应用程序位于 Bitrix24 中的 iframe 时,我无法登录用户。在这种情况下,如何在会话中存储内容?

Django CSP (pip install django-csp) 和 SESSION_COOKIE_SAMESITE = None 一起解决问题。

对于 django-csp:

CSP_DEFAULT_SRC = ["'self'"]
CSP_FRAME_ANCESTORS = ["'self'", 'https://*.example.com']

这将允许 Django 应用在所有 example.com 子域中显示。