【发布时间】:2016-09-27 14:43:40
【问题描述】:
我有一个视图,它将分页对象(在查询集上)发送到模板,我进一步在模板中将其呈现为表格。我要做的是单击模板上分页栏上的页码,它应该进行 ajax 调用以获取该页码的分页输出并用它动态更新表的内容。
查看:
def accounts(request):
#Including only necessary part
accounts_list = Accounts.objects.all()
paginator = Paginator(accounts_list, 25)
page = request.GET.get('page')
try:
accounts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
accounts = paginator.page(1)
except EmptyPage:
# If page is out of range, deliver last page of results.
accounts = paginator.page(paginator.num_pages)
context['accounts'] = accounts
return render(request, template, context)
模板将其加载为:
{% if accounts %}
<table id="acc">
<tr>
<th>Field 1</th>
...
<th>Field N</th>
</tr>
{% for item in accounts %}
<tr>
<td>{{ item.field1 }}</td>
...<!-- Some complex logic with template tags too in here -->
<td>{{ item.fieldN }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
现在对于分页栏,我使用Bootpag's library,我可以将内容渲染为:
$('.pagination_top').bootpag({
/*bootpag logic here */
}).on("page", function(event, num){
//$.ajax loading here where I can update the table div with new output
//or make the table div "template code" reload without reloading page
}
抱歉,我没有展示我在 ajax 部分尝试过的大部分内容,因为我对如何使模板重新渲染返回新帐户而不重新加载页面完全一无所知。
我能想到的唯一肮脏的解决方案是在视图中生成我的整个 html,然后使用返回的新 html ajax 更新表格 div 的 html?
在不重新加载页面的情况下使用模板呈现逻辑重新加载表格 div 的简单方法是什么?这可以通过使表格部分成为单独的模板并包含/扩展模板来实现吗?
请注意我不能使用模板上的所有数据,然后使用一些jquery/js libaray的分页逻辑的方法,因为完整的数据比较大。
【问题讨论】:
标签: jquery ajax django django-pagination