2012-05-06 197 views
0

我在嘗試對頁面進行分頁以便每頁顯示五個狀態。輸入這些代碼後,分頁失敗。以下是我的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 %} 
+0

我該怎麼做? – picomon

+0

還修復您的python代碼的縮進。 – rantanplan

回答

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}) 
+0

是的,我想通過過濾的數據分頁。這樣它將只顯示每個頁面五個狀態。我嘗試了上面的代碼,但是我得到:TemplateSyntaxError at/qaskp/ Caught TypeError while rendering:'Page'object is not iterable – picomon

+0

Now working!非常感謝! – picomon

+0

不客氣) –