CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-django-dynamic-preferences

Dynamic global and instance settings for your django project

Pending
Overview
Eval results
Files

django-integration.mddocs/

Django Integration

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.

Capabilities

Template Context Processor

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
    """

Configuration

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',
            ],
        },
    },
]

Usage Examples

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>

Performance Considerations

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.

Import Statement

from dynamic_preferences.processors import global_preferences

Integration Notes

  • The context processor automatically provides all global preferences to every template
  • Preference keys use the section__name format in templates
  • Values are automatically deserialized to their proper Python types
  • Changes to preferences are immediately reflected in templates due to cache invalidation
  • No additional view code needed - preferences are available in all templates once configured

Install with Tessl CLI

npx tessl i tessl/pypi-django-dynamic-preferences

docs

admin-integration.md

core-models.md

django-integration.md

forms-views.md

index.md

preference-types.md

registries.md

rest-api.md

serialization.md

signals.md

user-preferences.md

tile.json