Skip to main content

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

Project description

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

Implementation Details

When you use the {% include_by_ajax template_name %}, the page is loaded and rendered twice: once it is loaded with empty placeholders <section class="ajax-placeholder"></section>. Then it is loaded by Ajax again, and the placeholders get the data rendered. When the second load is complete, JavaScript replaces all the placeholders with their content. At 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.

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 staticfiles %}
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script src="{% static "include_by_ajax/js/include_by_ajax.js" %}" defer></script>
    
  4. In your page template, load and use the template tag for all content that is below the visible are 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" %}
    {% 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 cup of refreshing ice tea.

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

django-include-by-ajax-0.2.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

django_include_by_ajax-0.2.0-py2.py3-none-any.whl (5.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-include-by-ajax-0.2.0.tar.gz.

File metadata

  • Download URL: django-include-by-ajax-0.2.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.10

File hashes

Hashes for django-include-by-ajax-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ea0a6765f42620e060e0dd8dfbc84b34b5f2e73014f3ac31bccdafe58332f4b3
MD5 f26a225ba50512ab5ae9d2ef0ddce4b8
BLAKE2b-256 8dc7db447b9f110add7511e44580964ec05413698647f38283deb6bd280e5a7d

See more details on using hashes here.

File details

Details for the file django_include_by_ajax-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_include_by_ajax-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.10

File hashes

Hashes for django_include_by_ajax-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cfc467001a23b857a606ca5feebca9f4184ab3f6cbec6cd5d62754c3334846bc
MD5 4268bc6b5b6d527f9ffb950bd9695fd6
BLAKE2b-256 8fd432687efd2b07e8d8e0b3adfd15a62d5a9e8c88ee91e91b75134b6bedb1d7

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