Paginator that caches pages automatically.
Project description
django-cachedpaginator
============
# About
A paginator that caches pages and result count. Very handy when you need to paginate an expensive query
This application was inspired by https://djangosnippets.org/snippets/1173/
## Usage
### View
In your view:
```python
from django_cachedpaginator import Paginator, PageNotAnInteger, EmptyPage
def my_view(request):
cache_key = 'user_your_own_key'
cache_timeout = 600 # 10 minutes
count_timeout = 3600
page = request.GET.get('page')
object_list = ExpensiveModel.objects.all()
paginator = Paginator(object_list, 20, cache_key, cache_timeout, count_timeout)
try:
object_list = object_list.page(page)
except PageNotAnInteger:
object_list = object_list.page(1)
except EmptyPage:
object_list = object_list.page(image_list.num_pages)
```
Or in you CBV view:
```python
from django_cachedpaginator import CachedPaginatorViewMixin
class MyView(CachedPaginatorViewMixin, ListView):
....
paginate_by = 7
cache_timeout = 60
cache_key = "my_view"
```
And it will automatically caches the pagination.
## Templates
IMPORTANT: Make sure you add 'django_cachedpaginator' to your INSTALLLED_APPS settings, otherwide django cannot load the tag
All you need to do is to load the tags and pass your object_list to paginator tag like so:
```twig
{% load cached_paginator %}
...
{% paginate object_list %}
```
If you want to override the template that is used to render the paginator, you have a couple of options:
* Either create a file in your template folder (the one you've set in your settings file) by this name: django_cachedpaginator/paginator_template.html
* or set this variable in your settings file: PAGINATOR_TEMPLATE = 'path/to/your/template'
# Notes
cache_timeout nad count_timeout are measured in seconds and are optional. Defaults are 300 and 600.
There is no need to add the page in cache_key because Paginator takes care of it. Actually here is a list of variables added to key:
* cache_key
* per_page
* page_number
* cache_timeout
* count_timeout
Therefore when you change any of these, your previous cache expires.
============
# About
A paginator that caches pages and result count. Very handy when you need to paginate an expensive query
This application was inspired by https://djangosnippets.org/snippets/1173/
## Usage
### View
In your view:
```python
from django_cachedpaginator import Paginator, PageNotAnInteger, EmptyPage
def my_view(request):
cache_key = 'user_your_own_key'
cache_timeout = 600 # 10 minutes
count_timeout = 3600
page = request.GET.get('page')
object_list = ExpensiveModel.objects.all()
paginator = Paginator(object_list, 20, cache_key, cache_timeout, count_timeout)
try:
object_list = object_list.page(page)
except PageNotAnInteger:
object_list = object_list.page(1)
except EmptyPage:
object_list = object_list.page(image_list.num_pages)
```
Or in you CBV view:
```python
from django_cachedpaginator import CachedPaginatorViewMixin
class MyView(CachedPaginatorViewMixin, ListView):
....
paginate_by = 7
cache_timeout = 60
cache_key = "my_view"
```
And it will automatically caches the pagination.
## Templates
IMPORTANT: Make sure you add 'django_cachedpaginator' to your INSTALLLED_APPS settings, otherwide django cannot load the tag
All you need to do is to load the tags and pass your object_list to paginator tag like so:
```twig
{% load cached_paginator %}
...
{% paginate object_list %}
```
If you want to override the template that is used to render the paginator, you have a couple of options:
* Either create a file in your template folder (the one you've set in your settings file) by this name: django_cachedpaginator/paginator_template.html
* or set this variable in your settings file: PAGINATOR_TEMPLATE = 'path/to/your/template'
# Notes
cache_timeout nad count_timeout are measured in seconds and are optional. Defaults are 300 and 600.
There is no need to add the page in cache_key because Paginator takes care of it. Actually here is a list of variables added to key:
* cache_key
* per_page
* page_number
* cache_timeout
* count_timeout
Therefore when you change any of these, your previous cache expires.
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
Close
Hashes for django-cachedpaginator-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88eb47c8ac6a2195e3fb3a0445a2011b2716b29d7527b4452a63af53445c9231 |
|
MD5 | a7ccd8a5035b553dba584146dbf261d2 |
|
BLAKE2b-256 | 383c554d9ffd941354592e52ddc8471e647dde8be4a511862756f4b9369c915b |