Extending different base template for ajax requests in Django

Posted on July 13, 2011 in archive, django, ajax, templates, generic views, cbv.

While there are different ways to specify what should be rendered what should server return when ajax request is performed, using yesno template filter can be simplest.

This can be especially useful in situations when using without javascript should fail gracefully or when working with existing views.

 {% extends request.is_ajax|yesno:“base_ajax.html,base.html” %}

For this to work, django.core.context_processors.request should be installed in TEMPLATE_CONTEXT_PROCESSORS.

Share on Reddit