Problem
I need Ajax pagination using jQuery in a Django template.
Situation
I have the following code in my template:
<script type="text/javascript"> $(document).ready(function() { $("#next-page").click(function() { var page = {{ vms.next_page_number }}; $("#vms").html(' ').load ( '{% url virtualmachine-list %}?page=' + q ); }); }); </script> [code omitted] <table> <thead> [code omitted] </thead> <tbody id="vms"> {% for vm in vms.object_list %} [code omitted] {% endfor %} </tbody> </table> [code omitted] {% if vms.has_next %} <!--<a href="?page={{ vms.next_page_number }}" id="next-page">Next</a>--> <a href="#" id="next-page">Next</a> {% endif %} </span>
and my opinion:
def list_(request): vms = VirtualMachine.objects.all() paginator = Paginator(vms, 10) page = 1 if request.is_ajax: query = request.GET.get('page') if query is not None: page = query try: vms = paginator.page(page) except (EmptyPage, InvalidPage): vms = paginator.page(paginator.num_pages) return render_to_response('virtual_machine/list.html', { 'vms': vms, }, context_instance=RequestContext(request), )
Conclusion
So, whenever I click Next, it actually executes an Ajax request, but the data does not appear in the table.
Django.core.paginator is used for pagination, and I really would like to stick with it whenever possible.
Do you see that the code is missing?
jquery ajax django django-templates
Kenny meyer
source share