我正在尝试对页面进行分页,以便每页显示五种状态。输入这些代码后,它无法分页。以下是我的django应用程序中分页和更新状态的代码。
查看:
def qask(request):
extra_data_context={}
#if there's nothing in the field do nothing.
if request. method=="POST":
form =AskForm(request.POST)
if form.is_valid():
data=form.cleaned_data
newask=Ask(
user= request.user,
status=data['status'],
pub_date=datetime.datetime.now())
newask.save()
extra_data_context.update({'AskForm':form})
else:
form = AskForm()
extra_data_context.update({'AskForm':form})
extra_data_context.update({'Asks':Ask.objects.filter(user=request.user)})
plan=Ask.objects.all()
paginator=Paginator(plan, 5)
try:
page=int(request.GET.get('page','1'))
except ValueError:
page=1
try:
fp=paginator.page(page)
except (EmptyPage, InvalidPage):
fp=paginator.page(paginator.num_pages)
return render_to_response ('quik_ask.html',extra_data_context,context_instance=RequestContext(request))
模板:
{% block content %}
{% for Ask in Asks %}
<tr>
<p> {{Ask.user}} </p> </strong>
<p>{{Ask.status}}</p>
<p> {{Ask.pub_date|timesince }} ago </p>
</tr>
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if Asks.has_previous %}
<a href="?page={{ Asks.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ Asks.number }} of {{ Asks.paginator.num_pages }}.
</span>
{% if Asks.has_next %}
<a href="?page={{ Asks.next_page_number }}">next</a>
{% endif %}
</span>
</div>
{% endblock %}
答案 0 :(得分:0)
您的页面数据位于fp
变量中,但永远不会提交给您的模板。相反,您试图从Asks
获取页面数据,但它与代码中的paginator无关。我不确定你要分页的是什么,但是如果你想对你过滤的数据进行分页,它应该是这样的:
plan = Ask.objects.filter(user=request.user)
paginator=Paginator(plan, 5)
###...get you page number
try:
asks = paginator.page(page)
except (EmptyPage, InvalidPage):
asks = paginator.page(paginator.num_pages)
extra_data_context.update({'Asks': asks})