Skip to main content
Server-side script loading package for Python applications, providing secure, cached script loading with Django template tag integration.

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:
Every rendered <script> tag must include a data-code attribute set to your Account ID (verification code). AdUnblock uses this value to verify that the script is running on your registered domain. Find your Account ID at the top of your AdUnblock dashboard.
Add the server_tag app to your INSTALLED_APPS in your Django settings.py:

Cache Configuration

Configure caching in your settings.py (recommended for production):

Redis Cache (Production)

Local Memory Cache (Development)

Basic Usage

In your view, pass the Account ID / verification code via the template context:
Then in your Django template, load the server_tag_tags and use the server_tag tag with the script_attributes argument:

Custom Rendering

You can provide a custom Python function to the render_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 your settings.py:

Template Tag Parameters

The server_tag template tag accepts these parameters:

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

  1. Use Redis: Configure Redis caching for production environments
  2. Cache Warming: Pre-warm caches during deployment
  3. Error Monitoring: Monitor failed requests and adjust retry logic
  4. 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

Common Patterns

Base Template with Server Tags

Page-Specific Scripts

Next Steps

Test Implementation

Test your Django integration with our Chrome extension

Node.js Integration

Learn about Node.js server-side integration