S
S
Starrow2011-02-18 04:43:10
Django
Starrow, 2011-02-18 04:43:10

Django template inheritance and ajax?

I just started looking towards Python + Django, so I'm familiar with them at the level of reading the documentation.
As far as I understand, Django {% extends base.html %}should be the first line in templates. However, I would like to be able to not inherit from the base template if this is an ajax request. Apparently, the condition cannot be written here, so at the moment only this option is obvious:
In the view, we check if the request is not ajax. If ajax - render, for example, view-ajax.html. Otherwise, view.htmlin which:

{% extends base.html %}<br/>
###<br/>
{% include view-ajax.html %}

However, this option does not look attractive. How, let's say:
{% notajaxextends base.html %}
But for such a seemingly obvious behavior, writing, as I now see it, is not the easiest template tag - it seems doubtful. Pretty sure there are solutions. And besides, with extends we redefine the blocks of the base template. And since there is nothing to redefine, what then should be output? Pass required block as tag parameter?
{% notajaxextends base.html content %}<br/>
###<br/>
{% block content %} ... {% endblock %}

Can you help an aspiring jungist solve this ridiculous dilemma?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
kmike, 2011-02-18
@kmike

And why is the inclusion option bad? Use it.

A
anatoly_rr, 2011-02-18
@anatoly_rr

You can base.htmlcheck the flag, and if it is set, hide everything that is outside the {% block content %}. Not very pretty, but simpler than a tag.

F
FlyinKilla, 2011-02-18
@FlyinKilla

It is possible to render a specific block from a template. Maybe these links will help you:
http://djangosnippets.org/snippets/769/
http://djangosnippets.org/snippets/1014/

N
nwalker, 2011-05-02
@nwalker

{% extends is_ajax|yesno:'ajax_base.html,base.html' %}
where is_ajax = True for an ajax request.
I don't know which version will work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question