Skip to main content

A few utilities for making pagination and tabbing easier. Everything should work with and without javascript. If javascript is enabled, several features like preloading can be obtained.

Currently, we have:
  1. pagination

  2. tabbing

  3. xhr: for loading a part of the page delayed through AJAX, when it is heavy

    to render.

DEPENDENCIES:

1. Pagination

In settings.py, add this application:

> INSTALLED_APPS += (‘django_ajax’,)

The default styling just renders previous and next buttons. If you want links to more pages rendered, add this setting:

> PAGINATION_STYLE = ‘verbose’

In views.py

> from django_ajax.pagination import paginate > > # … read objects (Query list or other iterable object) > objects = range(0, 100) > > # Paginate > paginated_objects = paginate(request, objects, 25, > query_string_parameters= {‘order_by’: order_by, ‘by’: by }, > use_get_parameters=False, > page_variable=’page1’) > > context = { ‘paginated_objects’: paginated_objects }

25 tells the paginator to show no more than 25 objects per page. use_get_parameters is optional, when set True, it will add all the parameters of the current query string (request.METAQUERY_STRING) automatically to the paginator links. query_string_parameters is also optional, this are other parameters to add to the query string of the links. page_variable is optional too. It’s only required when several paginators are combined at the same page. The variable should be unique for each paginator.

In the template:

> {% load ajax_utilities %} > {% paginate paginated_objects %} > … > {% for object in paginated_objects.object_list %} > … > {% endfor %} > … > {% endpaginate %}

In the base template, add this script:

> <script type=”text/javascript” src=”{{ STATIC_URL }}js/ajax-utilities/pagination.js”></script>

This javascript code will look for the previous and next link, and it will preload these two pages. When one of these links is clicked, the paginator body of the preloaded page shall be put into the body of the currently visible page.

If you have additional navigation links inside the paginator body, like ordering column headers. Place the links in a container, classnamed “pagination-helper”. This will make them use ajax too.

> <div class=”pagination-helper”> > <a href=”{{ request.path }}?order_by_name”>…</a> > </div>

The search results of a GET form can be retreived through AJAX, similar to loading any other page. The form should have action=”?” and method=”get” parameters. When enter has been pressed in this form, only the body of the paginator will be reloaded, leaving the rest of the web page intact.

> <form method=”get” action=”?” class=”pagination-form”> > <label><input type=”text” name=”q” value=”{{ q }}” /></label> > </form>

In Javascript:

If you ever need to execute custom JS code when the paginator has replaced its body; use the following javascript code:

> function handler(e, containers) > { > // code to be excuted when the paginator switches from page … > // containers is an Array of elements (jquery items) where content may have been changed. > } > $(document).bind(‘paginatorPageReplaced’, handler);

If you want to delay the rendering of the paginator, because it contains some heavy queries, nest the paginator in an {% xhr %} template tag as follows: (Nesting the other way around is not yet possible, and probably never will be.)

> {% load ajax_utilities %} > {% xhr %} > {% paginate paginator %} > … > {% endpaginate %} > {% endxhr %}

Additional note: To allow bookmarking of pages, the currently visible page is appended to window.location.hash

1. Tabbing

In the template:

> {% load ajax_utilities %} > {% tabpage %} > {% tabs %} > <ul> > <li class=”selected”><a href=”…”>…</a></li> > <li class=””><a href=”…”>…</a></li> > <li class=””><a href=”…”>…</a></li> > </ul> > {% endtabs %} > {% tabcontent %} > {% block tab_content %} > inherit this template and fill in the tab content by creating a block named ‘tab_content’. > {% enblock %} > {% endtabcontent %} > {% endtabpage %} > > <script type=”text/javascript” src=”{{ STATIC_URL }}js/ajax-utilities/tabbing.js”></script>

This is usually the base template. Every tab matches a separate view, which renders a template, derived from this base template. It will fill in the block tab_content, and mark the tab as “selected”.

If you want a link inside a tab page to be opened inside the same tab, use the tabbing-helper-class:

> <a class=”tabbing-helper” href=”…”> (something which opens in this tab) </a>

In Javascript:

> function handler(e, containers) > { > // code to be excuted when the paginator switches from page … > // containers is an Array of elements (jquery items) where content may have been changed. > } > $(document).bind(‘tabLoaded’, handler);

1. xhr

This is an AJAX optimazation for heavy web pages. The {% xhr %} template tags marks a section of the template to be rendered during a later XML HTTP Request (AJAX).

So, actually, every view which template contains {% xhr %} is rendered twice. First without the slow, heavy content, where a placeholder <div> is inserted, and later on, rendering everything.

Insert the following scripts to your HTML:

> <script type=”text/javascript” src=”{{ STATIC_URL }}/js/ajax-utilities/xhr.js”></script>

Mark the slow parts with {% xhr %}:

> {% load xhr %} > … some content … > > {% xhr %} > Very slow content > {% else %} > Loading, … (insert loading message or images, … here) > {% endxhr %} > > … some content …

The {% else %} part can be omitted, or it can contain a loading message.

If the slow content depends on calculations in the view, which are not necessary for the other content. Make sure to not execute them during the first run in the view:

> def view(request): > if ‘xhr’ in request.REQUEST: > do_slow_calculations() > > return …

In Javascript:

> function handler() > { > … (code to be excuted when the xhr content has been loaded) … > $(‘.xhr_container’).each(function () { … } ); > } > $(document).bind(‘xhrLoaded’, handler);

Metadata

Release files for django-ajax-utilities 1.2.9

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-ajax-utilities 1.2.9
File Size Uploaded
django-ajax-utilities-1.2.9.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-ajax-utilities 1.2.9
File Interpreter ABI Platform
django_ajax_utilities-1.2.9-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 72.7 kB

Release files / django-ajax-utilities-1.2.9.tar.gz

Download URL django-ajax-utilities-1.2.9.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c4c2417105ab0a0b7b03080498f495de09c692f0e3ecccf2f77f809ca4ee42c2
BLAKE2b-256 checksum
How to use checksums
63520ef7c1a11922c97976cdfa1d6b70d664f89c11731160ce17d17eeda009f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_ajax_utilities-1.2.9-py2.py3-none-any.whl

Download URL django_ajax_utilities-1.2.9-py2.py3-none-any.whl
Size 64.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
25c568b048498d34a3803a06026957587a5c7622e725fd0b1414a3b6cc01dc3d
BLAKE2b-256 checksum
How to use checksums
2da6c6c76fbe8d65982bed3b11c8d00e544c0867bff1375e14a3be7091e23120
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.2.9 This release

2 release files

1.2.8

2 release files

1.2.7

3 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