Skip to main content

pypi

A Django App Providing the {% include_by_ajax %} Template Tag

The Problem

Start pages usually show data aggregated from different sections. To render a start page might take some time if the relations and filters are complex or if there are a lot of images. The best practice for performance is to display the content above the fold (in the visible viewport area) as soon as possible, and then to load the rest of the page dynamically by JavaScript.

The Solution

This app allows you to organize heavy pages into sections which are included in the main page template. The default including can be done by the {% include template_name %} template tag and it is rendered immediately. We are introducing a new template tag {% include_by_ajax template_name %} which will initially render an empty placeholder, but then will load the content by Ajax dynamically.

The template included by {% include_by_ajax template_name %} will get all the context that would normally be passed to a normal {% include template_name %} template tag.

You can also pass a placeholder template which will be shown until the content is loaded. For this use {% include_by_ajax template_name placeholder_template_name=placeholder_template_name %}

Implementation Details

When you use the {% include_by_ajax template_name %}, the page is loaded and rendered twice:

  • At first, it is loaded and rendered minimally with empty placeholders <section class="ajax-placeholder"></section>.
  • Then, some JavaScript loads it fully by Ajax and replaces placeholders with their content.

The templates that you include by Ajax can contain <style> and <script> tags which will be executed when loaded.

In the end, 'include_by_ajax_all_loaded' event is triggered for the document so that you can further initialize JavaScript functions.

Caveats

The templates that are included by {% include_by_ajax template_name %} should always wrap the content into a single html tag, like <div>, <span>, <section>, <article> or other.

Requirements

The app works with Django 2.2+ on the server and jQuery 3.x in the frontend.

Installation and Configuration

  1. Install the library to your virtual environment:

    (venv)$ pip install django-include-by-ajax
    
  2. Add 'include_by_ajax' to INSTALLED_APPS.

  3. In your base template, link to jQuery and include_by_ajax.js:

    {% load static %}
    <script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
    <script src="{% static 'include_by_ajax/js/include_by_ajax.min.js' %}" defer></script>
    
  4. In your page template, load and use the template tag for all content that is below the visible area of the page.

    {% extends "base.html" %}
    {% load include_by_ajax_tags %}
    
    {% block content %}
        <h1>My Website</h1>
        {% include "slideshows/includes/start_page_slideshow.html" %}
        <!-- the fold -->
        {% include_by_ajax "blog/includes/latest_blog_posts.html" %}
        {% include_by_ajax "news/includes/latest_news.html" %}
        {% include_by_ajax "gallery/includes/latest_pictures.html" placeholder_template_name="utils/loading.html" %}
    {% endblock %}
    
    {% block js %}
        <script>
        $(document).on('include_by_ajax_all_loaded', function() {
            console.log('Now all placeholders are loaded and replaced with content');
        })
        </script>
    {% endblock %}    
    
  5. Enjoy the faster-appearing web page at a glass of gingerbread latte.

Contributors

Thanks to everybody who contributed to this project:

Contributors

Release files for django-include-by-ajax 3.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-include-by-ajax 3.0.2
File Size Uploaded
django-include-by-ajax-3.0.2.tar.gz 14.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-include-by-ajax 3.0.2
File Interpreter ABI Platform
django_include_by_ajax-3.0.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 38.1 kB

Release files / django-include-by-ajax-3.0.2.tar.gz

Download URL django-include-by-ajax-3.0.2.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a584031fe176334ed9ee11f0209bff8d5098c368bb8ad3d878c16fe24fb999e3
BLAKE2b-256 checksum
How to use checksums
924661bcb647d18f0b6346bcf4015574f541ecbb27433e42aac41e4b01a5ccc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.0

Release files / django_include_by_ajax-3.0.2-py2.py3-none-any.whl

Download URL django_include_by_ajax-3.0.2-py2.py3-none-any.whl
Size 23.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ed29a049a40127090ab17ef29e5bf5b1fe029ae69e6092357ba6d8623086d053
BLAKE2b-256 checksum
How to use checksums
0adca0aedf31631b85b007c91e4fe08eb3e6d301c85bf91d4d0d049482fecd7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.0

Release history Release notifications | RSS feed

This release

3.0.2 This release

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page