Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Redis based counters for Django models
branch: master
Failed to load latest commit information.
counters Better post_delete signal handling
README.rst Fixed list() view

README.rst

Redis Counters

Counters for django models using redis.

Setup

settings.py:

REDIS = {
    'host': 'localhost',
    'port': 6379,
    'db': 1,
    'socket_timeout': 2,
}

COUNTERS_REDISES = {
    'default': dict(REDIS, db=4),
    'krasnoyarsk': dict(REDIS, host='krasnoyarsk.yoursite.ru', db=4, socket_timeout=5),
    'moscow': dict(REDIS, host='moscow.yoursite.ru', db=4, socket_timeout=5)
}
COUNTERS_REDISES.pop(SERVER_LOCATION)

Usage

models.py:

import counters

@counters.add('hits')
class Item(models.Model):
    ...

views.py:

def list(request):
    items = Item.objects.filter(...)
    counters.fill(items, 'hits') # queryset or list of objects
    ...

def detail(request, pk):
    item = Item.objects.get(pk=pk)
    item.incr_hits()

list.html:

{% for item in items %}
    {{ item.title }} {{ item.hits }}<br>
{% endfor $}

detail.html:

<h1>{{ item.title }}</h1>

Hits: {{ item.hits }}
Something went wrong with that request. Please try again.