Tomas
Tomas

Reputation: 490

Django - load page fragments using Ajax/Ajah

I have a complex page where some parts of the page take more time to load. I was thinking about using AJAH approach and load fragments using jQuery after initial page is loaded. Then I realized that should be quite common problem. How about some something like {% includeajax "sometemplate.html" %}?

What this means is load "sometemplate.html" using AJAH/AJAX. Wouldn't that be cool tag? Any ideas?

I know how to implement this, it would be nice to have reusable tag like this. Do you know about some other simple way to achieve this?

Thanks

Upvotes: 2

Views: 1121

Answers (2)

culebrón
culebrón

Reputation: 36473

I made a middleware and a template tag to do this:

{% delayed_block big_table %}
    Real content. It will be loaded by Javascript at document.ready event.
{% delayed_stub %}
    Stub content that is rendered immediately.
{% enddelayed_block %}

When the page is loaded, a piece of Javascript notices a special class and requests it from the server, makes a request with ?partial=big_table parameter. When big_table is in partial parameters, this block renders the real content. The middleware catches the output, extracts only this block and wraps it in JSON. Then the Javascript module puts it into the right place.

The project is called django-partial-page. The django part of it is an egg, and the Javascript module is in example/media/js/partial.js.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798774

I'd make it point to a view instead of a template, in the style of url. The template tag could generate a fake HttpRequest that it could send to the view, and then include the content of the response in the template.

Upvotes: 0

Related Questions