I\'ve been trying to understand what\'s the optimal way to do Ajax in Django. By reading stuff here and there I gathered that the common process is:
formul
Here is how I use the same template for traditional rendering and Ajax-response rendering.
Template:
<div id="sortable">
{% include "admin/app/model/subtemplate.html" %}
</div>
Included template (aka: subtemplate):
<div id="results_listing">
{% if results %}
{% for c in results %}
.....
{% endfor %}
{% else %}
The Ajax-view:
@login_required
@render_to('admin/app/model/subtemplate.html')#annoying-decorator
def ajax_view(request):
.....
return {
"results":Model.objects.all(),
}
Of course you can use render_to_response. But I like those annoying decorators :D
You can use jquery.load()
or similar, generating the HTML on the server and loading it into the DOM with JavaScript. I think someone has called this AJAH.