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 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.0.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.0.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for adunblock_server_tag_django-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7784d4bfe2ad2ba9fc98bc6fd37eccc2d61586ca68418e126fb3322b2aef5b0d
MD5 35662657a0cda26cd3261b3d323fe170
BLAKE2b-256 1cbf21c0a287f313493b5815b374850d2460ffacbef82f2a320d2ef282456579

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for adunblock_server_tag_django-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb412b827a5f1f798e60a7ce761c96e10e9551bea5ee2fc77fc1389ba4562501
MD5 7919c8ce50cab902ca28362e25ddd567
BLAKE2b-256 1cf2d0936e1cb80a69e19f4a47a03c60c924d2261a85d8a897b61a37731f5464

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