我正在尝试对页面进行分页,以便每页显示五个状态。输入这些代码后,分页失败。以下是我的 django 应用程序中用于分页和更新状态的代码。
视图(views.py 文件):
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))
模板(html文件):
{% 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 %}