Sunday, June 30, 2013

Re: Change template block via AJAX

What I normally do is to have a include template that contains only the content I would like to render.
I example:

{% block page_content %}
    <div id="page_content">
        {% include 'xhr_page_content.html' %}
{% endblock %}

Using this approach you have isolated the content. Loading the page without ajax will just load the page as normal, and will include everything.
Now to make the ajax load all you need is to modify your view to check for request.is_ajax(), and if true, you change your view to render "xhr_page_content.html", instead of the full template.

That was the summary, hope it's clear enough.

On Saturday, 29 June 2013 17:54:56 UTC+1, Parin Porecha wrote:

I have an application containing a topbar and rest is content area.

In my 'base.html', I've defined two blocks -

  {% block topbar %}{% endblock topbar %}
  {% block page_content %}
        {% block sidebar %}
        {% endblock sidebar %}
        {% block main %}
        {% endblock main %}
    {% endblock page_content %}

These two blocks are then overridden by child templates.
The topbar contains a search input. The search query is sent via AJAX, and the results are loaded in respective child blocks through client-side templating.

Now, I want that whenever user enters a search query for a user via a radio button instead of default, The block 'page_content' should get overridden by another template 'user_results.html'. ( I'll know it client-side itself )
This can be done by redirecting to a path like '/search/user/', but that would involve reloading of the page.
Is this possible via an AJAX request, and then the server would load another template which will replace the 'page_content' block without page refresh ?

Parin Porecha

say Kifflom! and look forward to 17.09.13 !

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment