Skip to main content

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

adunblock_server_tag_django-1.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

adunblock_server_tag_django-1.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file adunblock_server_tag_django-1.1.0.tar.gz.

File metadata

File hashes

Hashes for adunblock_server_tag_django-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ac2fe026d4d49c10824260d576ea06674552ce9d971b6676c7e858a20fe0e113
MD5 9140311741adc834f2d1612ebb78bf64
BLAKE2b-256 645f1e2da0dccb24f40db21767b13d6ba6ef2b690bd17c0195425cdd389066d4

See more details on using hashes here.

File details

Details for the file adunblock_server_tag_django-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for adunblock_server_tag_django-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b4082d4c28612605e9aeac895d87f256f141aebc3c5172ed3bb0db63baf356d1
MD5 13e56015ebd82f9616176c490e9081ef
BLAKE2b-256 e9f242209f7c8af2b838e4e0c62004017a8d636e020048187c8546489ac98a51

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page