1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Django 閲覧数(PV数)の実装

Last updated at Posted at 2021-02-18

Djangoでは閲覧数(PV数)を簡単に実装できる.

models.py

class Post(models.Model):
    ...
    # 閲覧数
    views = models.PositiveIntegerField(default=0)
    ...
views.py

def post_detail(request, post_id):
    post = get_object_or_404(Post, id=post_id)
    post.views += 1 #閲覧数をインクリメント
    post.save()
    ...

参考

Django公式ドキュメント

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?