A Django package to fetch and render scripts from a remote URL with template tag integration and caching support.
Project description
Server Tag Django Package
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:
pip install adunblock-server-tag-django
Add the server_tag app to your INSTALLED_APPS in your Django settings.py:
INSTALLED_APPS = [
# ... other apps
'server_tag',
]
Configure caching in your settings.py (recommended for production):
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379/1',
}
}
# Or for development, use local memory cache:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
Usage
In your Django template, load the server_tag_tags and use the server_tag tag:
{% load server_tag_tags %}
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
{% server_tag "https://your-remote-url.com/scripts" %}
</head>
<body>
<h1>My Page</h1>
</body>
</html>
Custom Rendering
You can provide a custom Python function to the render_script parameter to customize how script tags are rendered:
# my_app/templatetags/custom_tags.py
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.simple_tag
def custom_script_renderer(js_files):
from django.utils.html import escape
scripts = [f'<script src="{escape(src)}" defer></script>' for src in js_files.get('js', [])]
return mark_safe('\n'.join(scripts))
{% load server_tag_tags %}
{% load custom_tags %}
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
{% server_tag "https://your-remote-url.com/scripts" render_script=custom_script_renderer %}
</head>
<body>
<h1>My Page</h1>
</body>
</html>
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
Requirements
- Python 3.8 or higher
- Django 3.2 or higher
- requests 2.25.0 or higher
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file adunblock_server_tag_django-1.0.0.tar.gz.
File metadata
- Download URL: adunblock_server_tag_django-1.0.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7784d4bfe2ad2ba9fc98bc6fd37eccc2d61586ca68418e126fb3322b2aef5b0d
|
|
| MD5 |
35662657a0cda26cd3261b3d323fe170
|
|
| BLAKE2b-256 |
1cbf21c0a287f313493b5815b374850d2460ffacbef82f2a320d2ef282456579
|
File details
Details for the file adunblock_server_tag_django-1.0.0-py3-none-any.whl.
File metadata
- Download URL: adunblock_server_tag_django-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb412b827a5f1f798e60a7ce761c96e10e9551bea5ee2fc77fc1389ba4562501
|
|
| MD5 |
7919c8ce50cab902ca28362e25ddd567
|
|
| BLAKE2b-256 |
1cf2d0936e1cb80a69e19f4a47a03c60c924d2261a85d8a897b61a37731f5464
|