我一直在尝试将 django paginator 实现到我的白板应用程序中,这样我就可以将图片分成不同的页面。
当我尝试在不同页面之间移动时会出现问题。我将每个页面限制为 1 个对象并上传了几张图片以测试页面之间的分页是否有效,但是当我尝试使用分页方法在不同页面之间移动时,它没有响应.
http://img854.imageshack.us/img854/3303/94627386.jpg
我一直在研究和测试通过 django 分页文档解决这个问题的方法,我认为问题出在我的模板中的分页模块方法上。
我的观点.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 })
我的船.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