A django app for quickly setting global context variables on all templates.
Project description
django-quick-context
A django app for quickly setting global context variables on all templates.
It also allows fetching model instances from the database directly in the templates, which can be very useful in certain cases such as when the views are controlled by a third party app. See Example 3 for more.
Installation
pip install django-quick-context
Then add 'quick_context.context_processors.quick'
to your context_processors
:
# settings.py
TEMPLATES = [
{
# ...
'OPTIONS': {
'context_processors': [
# ...
'quick_context.context_processors.quick',
],
},
},
]
Usage
Example 1
Since version 1.4, this app allows you to access your Django project's settings effortlessly.
So, in your templates you can do this to access the settings:
{{ quick.settings.SOME_VARIABLE }}
Example 2
Let's say you want to make a variable called currency
available in all templates.
Put this code in a file where it gets loaded on startup (such as project's or app's __init__.py
or apps.py
file).
from quick_context import ContextRegistry
ContextRegistry.register(name='currency', value='USD')
The quick_context
app makes a variable called quick
available to every template.
Every variable that you register with the ContextRegistry
, it will be available
as an attribute of the quick
variable.
So, you can access the currency
variable in the templates like this:
{{ quick.currency }}
Example 3
This is a more useful case for using quick_context
.
Suppose you have a Notice
model which allows you to display different notices
on different pages in your website. For example, 'Home' page has a different notice
and 'Contact' page has a different notice.
A sample Notice
model:
# models.py
class Notice(models.Model):
name = models.CharField(unique=True)
message = models.CharField()
# Let's create a few notices
Notice.objects.create(name='homepage_notice', message='...')
Notice.objects.create(name='contact_notice', message='...')
Notice.objects.create(name='about_notice', message='...')
Now, to include a notice in the template, you'll have to send it the template context from the view:
# views.py
def home_view(request):
notice = Notice.objects.get(name='homepage_notice')
context = {'notice': notice}
return ...
def contact_view(request):
# ... same as above ...
# repeat for other view ...
As you can see, this process is repetitve.
quick_context
provides a better way to do it.
First, register the Notice
model:
# models.py
from quick_context import ContextRegistry
class Notice(...):
...
ContextRegistry.register_model(name='notice', model=Notice, lookup_field='name')
Now, you can access a notice for a particular page without having to pass it from the views:
{{ quick.notice.homepage_notice.message }} -> displays message of "homepage_notice"
{{ quick.notice.contact_notice.message }} -> displays message of "contact_notice"
The lookup_field
must be a field which has unique=True
. This is important
because behind the scenes, quick_context
just does a Model.objects.get(...)
lookup
and get
expects a single result from the database. If there are more than one results,
Django will throw a MultipleObjectsReturned
exception.
Fetching multiple objects and filtering queryset
Since version 1.2, support for basic filtering has been added.
You can fetch multiple objects by applying a filter on the lookup field:
{% for notice in quick.notice.filter__icontains.homepage %}
{{ notice.message }}
{% endfor %}
The above is equivalent to:
Notice.objects.filter(name__icontains='homepage')
API
ContextRegistry.register(cls, name, value)
Register a global context variable.
name
: (string) Name of the variable.
value
: (any) A value for the variable. It can be anything: a string, a class,
dictionary, etc.
ContextRegistry.register_model(cls, name, model, lookup_field)
Register a model.
name
: (string) Name of the variable.
model
: (class) The model class.
lookup_field
: (string) Name of the field to use for the lookup. Ensure that this
field has unique=True
because quick_context
just does a Model.objects.get(...)
lookup and get
expects a single result from the database. If there are more than
one results, Django will throw a MultipleObjectsReturned
exception.
ContextRegistry.update(cls, name, value)
Updates the value of an already registered variable.
name
: (string) Name of the variable.
value
: (any) New value for the variable.
ContextRegistry.get_registry(cls)
Returns a list of all the registered variables.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-quick-context-1.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba3ae7f1191ea1e5e16d93875dabc7cf176e61d7134debcf2c43a35ceec40a2e |
|
MD5 | 8f04356bec1dcbeaf05ec5e8d4412454 |
|
BLAKE2b-256 | 419c0e63a650a8303ef28f034d34ac5c221ca54de7f470d1c42d7090fa8f7c55 |
Hashes for django_quick_context-1.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae6bdbeb924014c160d3e3767512449db6ee4caced0800a3fed390fef8d94d16 |
|
MD5 | 06bef6f4886963d1b978ff270e565f41 |
|
BLAKE2b-256 | 8d795c45469644b393c06b66cc22081cc6308319b842c9d8f92359fdfb1ca611 |