Source code for ppa.common.views
from django.utils.cache import patch_vary_headers
from django.views.generic.base import TemplateResponseMixin, View
[docs]
class AjaxTemplateMixin(TemplateResponseMixin, VaryOnHeadersMixin):
"""View mixin to use a different template when responding to an ajax
request."""
#: name of the template to use for ajax request
ajax_template_name = None
#: vary on X-Request-With to avoid browsers caching and displaying
#: ajax response for the non-ajax response
vary_headers = ["X-Requested-With"]
[docs]
def get_template_names(self):
"""Return :attr:`ajax_template_name` if this is an ajax request;
otherwise return default template name."""
# check if requested via ajax
if self.request.headers.get("x-requested-with") == "XMLHttpRequest":
return self.ajax_template_name
return super().get_template_names()