Session data lost after a HttpResponseRedirect

我是研究僧i 提交于 2021-01-27 09:33:31

问题


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}))

The view the above is redirected to:

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)

In the console, I get:

Session set to:  icle 
Session data:  Nothing!

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.


回答1:


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



来源:https://stackoverflow.com/questions/50873880/session-data-lost-after-a-httpresponseredirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!