mercredi 8 mai 2019

Does this `if` condition ever runs?

I am reading some codes and I am confused here:

class PostListView(ListView):
    # compressed

    def get_context_data(self, **kwargs):

        session_key = 'viewed_topic_{}'.format(self.topic.pk)  # <-- here
        if not self.request.session.get(session_key, False):
            self.topic.views += 1
            self.topic.save()
            self.request.session[session_key] = True           # <-- until here

        kwargs['topic'] = self.topic
        return super().get_context_data(**kwargs)

so the if condition checks that if there is no session with that key then increment self.topics.view by one.

I am confused here because whenever a user log in to the website their session will be created automatically and there are zero chances for being no session unless a user doesn't log in to the website. (Please notice that this project doesn't allow unauthenticated users to view the home page, sign in is a must.) Is this if condition will ever be executed?

Aucun commentaire:

Enregistrer un commentaire