Is there a function that is called when a user visits a view (specifically a DetailView
) in django. I am trying to count the number of views a particular page has (like StackOverflow
), and I want to know if there is a function that I can utilise.
If this is useful, here is the DetailView
:
class DetailQuestionView(DetailView):
model = Question
template_name = 'questions/questions-part/question-detail.html'
In all the generic class based views the dispatch
method is the entry point of the view logic, i.e. if the view is used dispatch
will be called. Also the HTTP method in lowercase i.e. get
, post
methods are also called depending on the requests method. Therefore you can override either of these two to suit your needs:
class DetailQuestionView(DetailView):
model = Question
template_name = 'questions/questions-part/question-detail.html'
def dispatch(self, request, *args, **kwargs):
# Your code
return super().dispatch(request, *args, **kwargs)