Under Development: This Python package is currently under active
development. If you need immediate support or have specific requirements,
please contact our support team for
assistance.
Django Integration
A Django package to fetch and render scripts from a remote URL with template tag integration and caching support.Installation
Install the package via pip:server_tag
app to your INSTALLED_APPS
in your Django settings.py
:
Cache Configuration
Configure caching in yoursettings.py
(recommended for production):
Redis Cache (Production)
Local Memory Cache (Development)
Basic Usage
In your Django template, load theserver_tag_tags
and use the server_tag
tag:
Custom Rendering
You can provide a custom Python function to therender_script
parameter to customize how script tags are rendered:
Create Custom Template Tag
Use Custom Renderer in Template
Expected Remote Response Format
The remote URL should return a JSON response in this format:Django Features
- Template Tag Integration: Easy-to-use Django template tag
- HTTP Client: Uses requests library for reliable HTTP operations
- Caching: Built-in Django cache integration with configurable TTL
- Error Handling: Graceful error handling with fallback to empty arrays
- Security: XSS protection with proper HTML escaping
- Django Integration: Proper Django app structure with apps.py
Advanced Usage
Template Tag with Cache Control
Conditional Script Loading
Using in Class-Based Views
Configuration Options
The Django package supports several configuration options:Settings Configuration
You can configure default settings in yoursettings.py
:
Template Tag Parameters
Theserver_tag
template tag accepts these parameters:
Parameter | Type | Default | Description |
---|---|---|---|
remote_url | string | Required | URL to fetch script configuration from |
cache_timeout | number | 300 | Cache duration in seconds |
render_script | function | None | Custom script rendering function |
Error Handling
The Django package includes robust error handling:Security Considerations
- URL Validation: Only HTTP and HTTPS URLs are allowed
- XSS Protection: All output is properly escaped using Django’s built-in functions
- Error Handling: Failed requests don’t break template rendering
- Caching: Reduces load on remote servers and improves performance
Performance Tips
- Use Redis: Configure Redis caching for production environments
- Cache Warming: Pre-warm caches during deployment
- Error Monitoring: Monitor failed requests and adjust retry logic
- CDN Usage: Use CDN URLs for better performance
Requirements
- Python 3.8 or higher
- Django 3.2 or higher
- requests 2.25.0 or higher