Skip to main content

Count visits using cache for Django models

Project description

Django Visit Count

Count visits using cache for Django models.

Installation

  1. Set-up Django's cache framework.

  2. Install the Python package.

    pip install django_visit_count
    

Usage

Use VisitCountMixin. It adds a visit_count field to your model.

from django_visit_count.mixins import VisitCountMixin

class MyBlogPost(VisitCountMixin, models.Model):
    ...

Create and run migrations on your model.

$ python manage.py makemigrations my_blog_app
$ python manage.py migrate my_blog_app

Count visits in your view like this:

def view_blog_post(request, post_id):
    post = get_object_or_404(MyBlogPost, pk=post_id)
    post.count_visit(request)
    ...

Advanced Usage

If you need more control, you can use is_new_visit function.

class MyBlogPost(models.Model):
    total_visits = models.PositiveIntegerField(default=0)
    ...
from django_visit_count.utils import is_new_visit

def view_blog_post(request, post_id):
    post = get_object_or_404(MyBlogPost, pk=post_id)

    if is_new_visit(request, post):
        post.total_visits = F("total_visits") + 1
        post.save(update_fields=["total_visits"])

    ...

You can pass an optional keyword argument session_duration (integer, number of seconds) to count_visit or is_new_visit.

Settings

Default settings:

VISIT_COUNT_DEFAULT_SESSION_DURATION = 5 * 60  # seconds

Development

  • Install development dependencies in your virtualenv with pip install -e '.[dev]'
  • Install pre-commit hooks using pre-commit install.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-visit-count-1.1.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

django_visit_count-1.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file django-visit-count-1.1.1.tar.gz.

File metadata

  • Download URL: django-visit-count-1.1.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.10

File hashes

Hashes for django-visit-count-1.1.1.tar.gz
Algorithm Hash digest
SHA256 637967204b55ff0c992854817964dbf36038e0ac1e5cae669222a593a42a2179
MD5 a1b8cd64f5880869d10997e0dfff6baf
BLAKE2b-256 b3b319792cfb055146f79acc4618f68de9e60f58fc64cd8b1e68489ff1be25b7

See more details on using hashes here.

File details

Details for the file django_visit_count-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_visit_count-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 25ac219bdf447ecda39245bf2b549f4944b7a454c73f88cd90b635af3da10c67
MD5 416f9eddeb95d0da6178bda0cc01e235
BLAKE2b-256 6575c3d3aefb347c5d3f169ad2e1506c90f623ab47d2e421f5237bc0497e2cb7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page