I am writing a site, and some pages are requested both with Ajax, or with a normal request. Of course, when requesting with Ajax, I’d like to render a slightly different template. I’ve tried several methods (A render_block snippet which was a great idea but buggy, using {% include %} to separate the templates while factorizing the code, using {% ifnot request.is_ajax %} to render content, like toolbar or menu, only if not Ajax). So I thought that it would be really cool to do like so :
<div> A menu that should not appear with Ajax </div>
{% ajax %}
<div> The Ajax part </div>
{% endajax %}
<div> A footer that should not appear with Ajax </div>
Then normal request returns :
<div> A menu that should not appear with Ajax </div>
<div> The Ajax part </div>
<div> A footer that should not appear with Ajax </div>
Ajax request :
<div> The Ajax part </div>
However, I don’t really now where to start … I’ve already written template tags, but never had to do something so complicated ! IS it even possible ?
That sounds like a really confusing way of doing it – I’d definitely stick to:
Alternatively, separate out your templates, so that the stuff you want rendered both times is in one template (we’ll call this
my_ajax_template.html):When responding to requests that come from AJAX your view code can just render that template, but when responding to “normal” requests, you could render a template that looked like this: