我在嘗試對頁面進行分頁以便每頁顯示五個狀態。輸入這些代碼後,分頁失敗。以下是我的django應用程序中用於分頁和更新狀態的代碼。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 %}
我該怎麼做? – picomon
還修復您的python代碼的縮進。 – rantanplan