Package: django-quick-context
django-quick-contextA 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.
SeeExample 3for more.Installationpip install django-quick-contextThen add'quick_context.context_processors.quick'to yourcontext_processors:# settings.pyTEMPLATES=[{# ...'OPTIONS':{'context_processors':[# ...'quick_context.context_processors.quick',],},},]UsageExample 1Since 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 2Let's say you want to make a variable calledcurrencyavailable in all templates.
Put this code in a file where it gets loaded on startup (such as project's or app's__init__.pyorapps.pyfile).fromquick_contextimportContextRegistryContextRegistry.register(name='currency',value='USD')Thequick_contextapp makes a variable calledquickavailable to every template.
Every variable that you register with theContextRegistry, it will be available
as an attribute of thequickvariable.So, you can access thecurrencyvariable in the templates like this:{{ quick.currency }}Example 3This is a more useful case for usingquick_context.Suppose you have aNoticemodel 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 sampleNoticemodel:# models.pyclassNotice(models.Model):name=models.CharField(unique=True)message=models.CharField()# Let's create a few noticesNotice.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.pydefhome_view(request):notice=Notice.objects.get(name='homepage_notice')context={'notice':notice}return...defcontact_view(request):# ... same as above ...# repeat for other view ...As you can see, this process is repetitve.quick_contextprovides a better way to do it.First, register theNoticemodel:# models.pyfromquick_contextimportContextRegistryclassNotice(...):...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"Thelookup_fieldmust be a field which hasunique=True. This is important
because behind the scenes,quick_contextjust does aModel.objects.get(...)lookup
andgetexpects a single result from the database. If there are more than one results,
Django will throw aMultipleObjectsReturnedexception.Fetching multiple objects and filtering querysetSince 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')APIContextRegistry.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 hasunique=Truebecausequick_contextjust does aModel.objects.get(...)lookup andgetexpects a single result from the database. If there are more than
one results, Django will throw aMultipleObjectsReturnedexception.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.LicenseBSD-3-Clause