Package: django-pjax
An improvement ofDjango-PJAX: The Django helper for jQuery-PJAX.What’s PJAX?PJAX is essentiallyAHAH(“Asynchronous HTML and HTTP”), except with
real permalinks and a working back button. It lets you load just a
portion of a page (so things are faster) while still maintaining the
usability of real links.A demo makes more sense, socheck out the one defunkt put together.CreditsThis project is an extension ofDjango-PJAXand all credits from the
original version goes toJacob Kaplan-Moss.AboutThis project keeps the original structure, but add new features to it,
and aims to keep django-pjax updated. Some goals are to keep this
project working with Python 2.7+ and 3.3+ and also Django 1.5+.Feel free to submit a PR and contribute to this project.CompatibilityPython 2.6+ or 3.2+PyPy or PyPy3CPythonDjango 1.3+Not all Django versions works with Python, PyPy or CPython. See the
Django docs to know more about supported versions.InstallJust run:pip installdjango-pjaxUsageFirst, read abouthow to use jQuery-PJAXand pick one of the
techniques there.Next, make sure the views you’re PJAXing are usingTemplateResponse.
You can’t use Django-PJAX with a normalHttpResponse, onlyTemplateResponse.PJAX decoratorThe pjax decorator:pjax(pjax_template=None,additional_templates=None,follow_redirects=False)pjax_template(str): default template.additional_templates(dict): additional templates for multiple
containers.follow_redirects(bool): if True, all django redirects will force a
page reload, instead of placing the content in the pjax context.Decorate these views with the pjax decorator:fromdjpjaximportpjax@pjax()defmy_view(request):returnTemplateResponse(request,"template.html",{'my':'context'})After doing this, if the request is made via jQuery-PJAX, the@pjax()decorator will automatically swap outtemplate.htmlfortemplate-pjax.html.More formally: if the request is a PJAX request, the template used in
yourTemplateResponsewill be replaced with one with-pjaxbefore the file extension. Sotemplate.htmlbecomestemplate-pjax.html,my.template.xmlbecomesmy.template-pjax.xml, etc. If there’s no file extension, the
template name will just be suffixed with-pjax.You can also manually pick a PJAX template by passing it as an argument
to the decorator:fromdjpjaximportpjax@pjax("pjax.html")defmy_view(request):returnTemplateResponse(request,"template.html",{'my':'context'})You can also pick a PJAX template for a PJAX container and use multiple
decorators to define the template for multiple containers:fromdjpjaximportpjax@pjax(pjax_template="pjax.html",additional_templates={"#pjax-inner-content":"pjax_inner.html")defmy_view(request):returnTemplateResponse(request,"template.html",{'my':'context'})Class-based viewIf you’d like to use Django 1.3’s class-based views instead, a PJAX
Mixin class is provided as well. Simply usePJAXResponseMixinwhere
you would normally have usedTemplateResponseMixin, and yourtemplate_namewill be treated the same way as above.You can alternately provide apjax_template_nameclass variable if
you want a specific template used for PJAX responses:fromdjango.views.genericimportViewfromdjpjaximportPJAXResponseMixinclassMyView(PJAXResponseMixin,View):template_name="template.html"pjax_template_name="pjax.html"defget(self,request):returnself.render_to_response({'my':'context'})That’s it!Using Template ExtensionsIf the content in yourtemplate-pjax.htmlfile is very similar to
yourtemplate.htmlan alternative method of operation is to use the
decoratorpjaxtend, as follows:fromdjpjaximportpjaxtend@pjaxtenddefmy_view(request):returnTemplateResponse(request,"template.html",{'my':'context'})Then, in yourtemplate.htmlfile you can do the following:{% extends parent %}
...
...Note that the template will extendbase.htmlunless its a pjax
request in which case it will extendpjax.html.If you want to define the parent for a standard http or pjax request,
you can do so as follows:fromdjpjaximportpjaxtend@pjaxtend('someapp/base.html','my-pjax-extension.html')defmy_view(request):returnTemplateResponse(request,"template.html",{'my':'context'})Using this approach you don’t need to create many*-pjax.htmlfiles.If you have a collision with the variable nameparentyou can
specify the context variable to use as the third parameter to pjaxtexd,
as follows:fromdjpjaximportpjaxtend@pjaxtend('someapp/base.html','my-pjax-extension.html','my_parent')defmy_view(request):returnTemplateResponse(request,"template.html",{'my':'context'})Which would require the following in your template:{% extends my_parent %}
...
...TestingInstall dependencies:pip install-rrequirements.txtRun the tests:python tests.py