Django Visit Count
Count visits using cache for Django models.
Installation
-
Set-up Django's cache framework.
-
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()
...
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
Release files for django-visit-count 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-visit-count-1.1.0.tar.gz | 4.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_visit_count-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.7 kB
Release files / django-visit-count-1.1.0.tar.gz
| Download URL | django-visit-count-1.1.0.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64fb6ce7b3564fbd8bb0e0f79e9dda8875c7f7f3a2f1132f0108117dba91c4b9
|
|
BLAKE2b-256 checksum How to use checksums |
64dec379d671a7f09e50099812e7379aaf5bfdbbb320186358be805271861c0e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.7
|
Release files / django_visit_count-1.1.0-py3-none-any.whl
| Download URL | django_visit_count-1.1.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b0368a2c4bce62752dd031daff888bb4cee41d7a50565fa1c0dbe7dd167196e9
|
|
BLAKE2b-256 checksum How to use checksums |
dd0fe64016869dc3c1d6c25807f6ebc697becb602b0adbd999b2165d1fb26601
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.7
|