Answer the question
In order to leave comments, you need to log in
How to display the news view counter correctly?
Good afternoon!
I decided to add an attribute of the counter for viewing a specific news to the model:
And I began to figure out how to make the counter increase when viewing a specific news.
I thought of the following:views = models.IntegerField(default=0)
class ViewNews(DetailView):
model = News
context_object_name = 'news_item'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['unwanted_link'] = 'news/news_detail.html'
context['news_item'].views += 1
context['news_item'].save()
return context
context['news_item'].views += 1
context['news_item'].views = F('views') + 1
F(views) + Value(1)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question