Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (46 sloc) 1.62 KB
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
from django import template
from django.http import QueryDict
from django.utils.encoding import smart_str
register = template.Library()
def pjax(template_names, request, default="pjax_base.html"):
Returns template name for request.
:param request: Django request or boolean value
:param template_names: Base theme name or comma-separated names of base and
pjax templates.
{% extends "base.html"|pjax:request %}
{% extends "base.html,pjax_base.html"|pjax:request %}
context = {"is_pjax": True}
{% extends "base.html"|pjax:is_pjax %}
if isinstance(request, (bool, int)):
is_pjax = request
is_pjax = request.META.get("HTTP_X_PJAX", False)
if "," in template_names:
template_name, pjax_template_name = template_names.split(",", 1)
template_name, pjax_template_name = template_names, default
if is_pjax:
return pjax_template_name.strip() or default
return template_name.strip()
def unpjax(url):
Removes `_pjax` param from query string of a given url::
<a href="{{ request.get_full_path|unpjax }}"></a>
if "?" in url:
base, qs = url.split("?", 1)
if "_pjax" in qs:
qs = QueryDict(smart_str(qs), mutable=True)
qs.pop("_pjax", None)
qs = qs.urlencode()
if qs:
return base + "?" + qs
return base
return url