Django Paginator错误

时间:2013-03-22 14:42:05

标签: django

我一直在尝试将django paginator实现到我的白板应用程序中,这样我就可以将图片分成不同的页面了。

当我尝试移动不同的页面时出现问题。我将每个页面限制为1个对象并上传了几张图片以测试页面之间的分页是否有效但是当我尝试使用分页方法移动到不同的页面时,它不会不回应。

http://img854.imageshack.us/img854/3303/94627386.jpg

我一直在通过django分页文档研究和测试这些问题的解决方案,我认为问题在于模板中的分页模块方法。

我的views.py

 def Boat(request ,animal_id):
         if not request.user.is_authenticated():
             return HttpResponseRedirect(reverse('world:LoginRequest'))

    picture = Picture.objects.filter(board=animal_id)

    paginator = Paginator(picture,1)
    page = request.GET.get('page')
    try:
             picture = paginator.page(page)

         except PageNotAnInteger:
             picture = paginator.page(1)
         picture = paginator.page(paginator.num_pages)


    return render(request,'boat.html',{'picture':picture })

我的boat.html

 {% if picture.object_list %}
 <ul>
    {% for pet in picture.object_list %}


        {% if pet.image %}

    <br>
        <img src= "{{ pet.image.url }}" </a>
 <br>
    </a>
 </li>
        {% endif %}

 <br>
 <a href="{% url world:CommentCreator pet.id %}">View Comment</a>  <a href="{% url      world:LikePicture pet.id %}">Like</a><br/>
 {% for c in picture %}
     {% ifequal c.picture.id pet.id %}

 <br>{{ c.body }}</li>
 <br>{{ c.created}}</li>
 <br>{{ c.user}}</li>
     {% endifequal %}
 % endfor %}

 {% endfor %}
 </ul>
 {% endif %}
 <a href="{% url world:PictureCreator %}">Add Pictures to your board</a><br/>

 {% if number %}
 {{number}}
 {% endif %}
 <a href="{% url world:Profile %}">Return back to Profile</a><br/>

 <br><br><br><br><br>

 <div class="pagination">
     <span class="step-links">
         {% if picture.has_previous %}
             <a href="?page={{ picture.previous_page_number }}">previous</a>
         {% endif %}

         <span class="current">
             Page {{ picture.number }} of {{ picture.paginator.num_pages }}.
         </span>

         {% if picture.has_next %}
             <a href="?page={{ picture.next_page_number }}">next</a>
         {% endif %}
     </span>
 </div>

我的模块的一部分

class Picture(models.Model):
    user = models.ForeignKey(User)
    board = models.ForeignKey(Board,blank=False,null=False,related_name='board')
    image = models.FileField(upload_to="images/",blank=True)
    description = models.TextField()
    is_primary = models.BooleanField(default=False)

    def __unicode__(self):
        return self.description

1 个答案:

答案 0 :(得分:1)

def Boat(request ,animal_id):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('world:LoginRequest'))

    picture = Picture.objects.filter(board=animal_id)

    paginator = Paginator(picture,1)

    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    try:
        picture = paginator.page(page)
    except (EmptyPage, InvalidPage):
        picture = paginator.page(paginator.num_pages)

    picture = paginator.page(paginator.num_pages)

    return render(request,'boat.html',{'picture':picture })