Dynamic global and instance settings for your django project
—
Django framework integration features including template context processors, template tags, and middleware support. These components provide seamless integration with Django's template system and web framework features.
Template context processor that automatically injects global preferences into template context, making them available in all templates without manual passing from views.
def global_preferences(request):
"""
Pass the values of global preferences to template context.
Args:
request: Django HttpRequest object
Returns:
dict: Dictionary with 'global_preferences' key containing all preference values
"""Settings Configuration:
# settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
# Add the global preferences context processor
'dynamic_preferences.processors.global_preferences',
],
},
},
]Template Usage:
<!-- In any Django template -->
<!DOCTYPE html>
<html>
<head>
<title>{{ global_preferences.general__site_title }}</title>
</head>
<body>
{% if global_preferences.general__maintenance_mode %}
<div class="maintenance-banner">
Site is currently under maintenance
</div>
{% endif %}
<main style="background-color: {{ global_preferences.ui__background_color }};">
<!-- Site content -->
<h1>{{ global_preferences.general__site_title }}</h1>
<p>Welcome message: {{ global_preferences.general__welcome_message }}</p>
</main>
</body>
</html>Accessing Nested Preferences:
<!-- For preferences with sections -->
<div class="theme-{{ global_preferences.ui__theme }}">
<h1>{{ global_preferences.site__title }}</h1>
{% if global_preferences.features__enable_comments %}
<div id="comments-section">
<!-- Comments functionality -->
</div>
{% endif %}
<footer>
Contact: {{ global_preferences.contact__email }}
</footer>
</div>Using with Template Filters:
<!-- Apply Django template filters to preference values -->
<p>Last updated: {{ global_preferences.site__last_updated|date:"F j, Y" }}</p>
<p>Site description: {{ global_preferences.site__description|truncatewords:20 }}</p>
<p>Admin email: {{ global_preferences.contact__admin_email|urlize }}</p>The context processor uses the cached preference manager, so template access to preferences is efficient and doesn't result in additional database queries on each request. The preferences are loaded once and cached until they are updated.
from dynamic_preferences.processors import global_preferencessection__name format in templatesInstall with Tessl CLI
npx tessl i tessl/pypi-django-dynamic-preferences