Reputation: 899
I have a Django template where it shows multiple values received from the database and passes it to the template, something like following:
in views.py
def test1(requests):
requests.get...
requests.get...
requests.get...
someQuery = "select id from table;"
executeQ(someQuery)
someQuery = "select id from table;"
executeQ(someQuery)
someQuery = "select id from table;"
executeQ(someQuery)
context = Data1, Data2, Data3
return render_to_response('test1/index.html', context)
in template/test1/index.html
<html>
......
<table>
<th> header1 </th>
<th> header2 </th>
<th> header3 </th>
{% for row in context %}
<td> row.1 </td>
<td> row.2 </td>
<td> row.3 </td>
{% endfor %}
Now, what I want is to update those row.1, row.2, row.3 via Ajax without realoding the page everytime. The data come from the database. So where and how can I put in some Ajax() so this happens with Django?
Upvotes: 2
Views: 2739
Reputation: 2406
You will need to add some client side code (JavaScript) in addition to you server side code (Python).
A common approach is to use the jQuery ajax()
method to send an ajax request to the server, handle this using your django application, send a response back to the client and then manipulate the DOM.
So your client side code needs to
$.ajax()
)$.ajax()
success callback
function)And your django application needs to
HttpResponse
(this SO question talks about JSON and HttpResponses)I also recommend you read this excellent answer about using django and ajax (including examples of the jQuery $.ajax()
method).
It's also worth mentioning that you do not have to use jQuery for the ajax step - you can generate Ajax requests with pure JavaScript - however it is a popular approach and pretty user friendly.
Upvotes: 7