I'm learning django.
I want to count the number of clicks to the URL on the page that puts the title and URL of the news article on the net as a model in mysql and displays it as a list.

class News (models.Model):
    news_title = models.TextField ()
    news_url = models.TextField ()
    view_number = models.IntegerField (blank = False, default = 0)

Actually on the page
I'm arranging the news in the display, and I want to increase the value of view_number when clicked so that the number of clicks can be displayed next to the article.
I don't know how to implement it, whether it's better to use javascript or write it in django's view, so I'd like to hear your opinion.

  • Answer # 1

    When writing to views
    This calls the DB directly in that function to increase the count
    Since it is called when accessing the URL, it will be counted even if you copy and paste the URL.
    So this is something like the number of impressions

    When saving with javascript
    When you click the link, it will be sent to the DB using ajax etc. and then skipped to the link.
    Unlike the above, this will remember the number of times the link was clicked, so
    If you copy the URL of news and access it, it will not be recorded

    So it seems that it depends on the application.