Search code examples
pythondjangodjango-class-based-viewsdjango-generic-viewsdjango-sessions

Django session not persisting with generic views


class IndexTemplateView(TemplateView):
    '''Index TemplateView.'''

    template_name = 'frontend/index.html'

    def post(self, request, *args, **kwargs):
        '''Manages credentials received for methods calling authentication.'''

        bitrix24_domain = request.GET.get('DOMAIN')
        request.session['bitrix24_domain'] = bitrix24_domain
        print(request.session['bitrix24_domain']) # String is stored and printed to the screen.
        return redirect('index')

    # Bitrix24 sends credentials via POST right after GET request.
    # CSRF protection would cause error in this case.
    @csrf_exempt
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)


class LoginTemplateView(TemplateView):
    '''Login TemplateView.'''

    template_name = 'frontend/login.html'

    def get(self, request, *args, **kwargs):
        '''Renders the login page.'''

        redirect_uri = get_google_redirect_uri()
        print(request.session.items()) # Returns empty session. The string was never saved.
        return redirect(redirect_uri)

I've tried setting request.session.modified = True but it didn't work either. I really don't know why I can't store a string in session. I've also tried to store in self.request within the POST request, but without success, tried to store it in the dispatch() function, in the setup() function. Tried almost everything and I can't store a single piece of information in my session. I'm also using Django Rest framework.

Can anyone help me on how to use sessions with generic views?


Solution

  • Problem solved, session does not work with tunneling service as Ngrok.