Skip to main content

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

BSD-3-Clause

Metadata

Release files for django-quick-context 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-quick-context 1.4.0
File Size Uploaded
django-quick-context-1.4.0.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-quick-context 1.4.0
File Interpreter ABI Platform
django_quick_context-1.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / django-quick-context-1.4.0.tar.gz

Download URL django-quick-context-1.4.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ba3ae7f1191ea1e5e16d93875dabc7cf176e61d7134debcf2c43a35ceec40a2e
BLAKE2b-256 checksum
How to use checksums
419c0e63a650a8303ef28f034d34ac5c221ca54de7f470d1c42d7090fa8f7c55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.5

Release files / django_quick_context-1.4.0-py3-none-any.whl

Download URL django_quick_context-1.4.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ae6bdbeb924014c160d3e3767512449db6ee4caced0800a3fed390fef8d94d16
BLAKE2b-256 checksum
How to use checksums
8d795c45469644b393c06b66cc22081cc6308319b842c9d8f92359fdfb1ca611
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page