HttpResponseRedirect后会话数据丢失 [英] Session data lost after a HttpResponseRedirect

查看:51
本文介绍了HttpResponseRedirect后会话数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置会话变量的重定向视图.但这就像在重新定向到该视图之前调用会话 flush 一样,因为整个会话在此处为空.

I have this redirect view that sets session variables. But it's as if the session is flushed before the view that is being redirected to is called, as the whole session is empty there.

(顺便说一句,这是真实的代码,我已经剪了一些东西以找出原因,但仍然找不到).

(Btw, this is the real code, I've cut stuff to find the cause but still can't find it).

class ActivateUserView(RedirectView):
    def get(self, request, *args, **kwargs):
        # activates user and redirects to listing
        listing = Listing.objects.get(id=2)
        request.session['test'] = 'icle'
        print("Session set to: ", request.session.get('test', "Nothing!"))
        return HttpResponseRedirect(reverse('listing-detail', kwargs={'pk': listing_pk, 'slug': listing.slug}))

上面的视图重定向到:

class ListingDetailView(TemplateView):
    template_name = "frontend/detail.html"

    @method_decorator(ensure_csrf_cookie)
    def get(self, request, *args, **kwargs):
        print("Session data: ", request.session.get('test', "Nothing!"))
        return super(ListingDetailView, self).get(request, *args, **kwargs)

在控制台中,我得到:

Session set to:  icle 
Session data:  Nothing!

我已经使用django-debug-toolbar进行了检查,在这里和那里以及在重定向调用和视图之间的某个地方引发了异常,所有会话数据都被删除了.

I've checked with django-debug-toolbar, raising Exceptions here and there, and somewhere between the redirect call and the view, all session data is deleted.

推荐答案

SESSION_COOKIE_SECURE 设置为False(在我的开发设置中)解决了此问题.我使用的是本地非https开发环境.

Setting SESSION_COOKIE_SECURE to False (in my dev settings) solved the issue. I was using a local, non-https dev environment.

这篇关于HttpResponseRedirect后会话数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆