Using Django class-based views, how can I return a different template if request.is_ajax

徘徊边缘 提交于 2019-11-30 09:56:10

The appropriate way to do this is to override the methods provided by the TemplateResponseMixin.

If you simply need to provide a different template for Ajax requests, then override get_template_names. If you want to provide a different response altogether, say a application/json response, then override render_to_response to produce a different HttpResponse for Ajax requests.

Chris Pratt

Override get_template_names:

def get_template_names(self):
    if self.request.is_ajax():
        return ['ajax_template.html']
    else:
        return ['standard_template.html']
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!