I’m posting this question not because I am getting some errors but because I would like to know how dajax should be used
Here is what I want to do:
I want some links on my homepage (eg. books, authors), when user clicks them, he gets a list of books/authors.
Here is what I’m doing:
When user clicks on books, I call a dajax process.
Inside ajax.py:
- I get all the books using
books = Book.objects.all() - I have a
template = "{% for book in books %}"... blah blah blah - I render the template
a = Template(template).render(Context(locals())) - I do dajax assign a as innerhtml of some div tag.
Is this the right way to do things?
Instead of writing the template in ajax.py should I open templates from some xyz.html and then render it? Any other good, clean ways to get similar tasks done?
Loading a template from a dedicated template file is in almost any case cleaner. A long python string with HTML in it is not very readable.
Of course you could also send your books via
add_data(data,callback_function)and write a javascript callback function that populates the list which would be the Dajax way of doing what Thomas Orozco proposed.But judging from the pagination example rendering a template file and sending it to
innerHTMLis ‘the right way’.