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 Script Attributes
Pass a script_attributes dict from your view context to render extra attributes (including data-*) on every generated <script> tag:
# views.py
def my_view(request):
return render(request, 'my_template.html', {
'script_attrs': {
'data-code': 'abc123',
'data-source': 'server-tag',
'defer': True,
},
})
{% load server_tag_tags %}
{% server_tag "https://your-remote-url.com/scripts" script_attributes=script_attrs %}
Rendering rules:
True→ bare attribute (e.g.defer)False/None→ omitted- any other value →
key="value"(HTML-escaped)
By default, every rendered script includes async. Set script_attributes={'async': False} to disable it.
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
Metadata
Release files for adunblock-server-tag-django 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| adunblock_server_tag_django-1.1.0.tar.gz | 4.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| adunblock_server_tag_django-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.1 kB
Release files / adunblock_server_tag_django-1.1.0.tar.gz
| Download URL | adunblock_server_tag_django-1.1.0.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ac2fe026d4d49c10824260d576ea06674552ce9d971b6676c7e858a20fe0e113
|
|
BLAKE2b-256 checksum How to use checksums |
645f1e2da0dccb24f40db21767b13d6ba6ef2b690bd17c0195425cdd389066d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.8
|
Release files / adunblock_server_tag_django-1.1.0-py3-none-any.whl
| Download URL | adunblock_server_tag_django-1.1.0-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b4082d4c28612605e9aeac895d87f256f141aebc3c5172ed3bb0db63baf356d1
|
|
BLAKE2b-256 checksum How to use checksums |
e9f242209f7c8af2b838e4e0c62004017a8d636e020048187c8546489ac98a51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.8
|