我正在制作一个有两个模型的画廊。像下面
class Gallery(models.Model):
name = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published', auto_now_add=True)
slug_name = models.CharField(max_length=200)
def __unicode__(self):
return self.name
class Image(models.Model):
gallery = models.ForeignKey(Gallery)
image = models.ImageField(upload_to='gallery/%Y/%m/%d')
caption = models.TextField(blank=True)
up_date = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.caption
现在我有一个视图,它将提供一个图像页面。我得到这样的图像
def image_page(request, slug_name, image_id):
image = Image.objects.get(pk = image_id)
all = Gallery.objects.get(slug_name = slug_name).image_set.all()
return render_to_response('gallery/image_page.html',
{
'image': image,
'all': all,
}, context_instance = RequestContext(request))
通过“图像”,我得到了那个单一的图像。通过“全部”,我得到了视图中可能需要用于显示分页的所有图像。分页仅意味着下一个和上一个按钮。
现在,如果此单页是第一页,则如果最后一页下一页按钮不应显示,则上一个链接不应显示类似。除此之外,两个链接都应该显示。
我的问题是如何在模板中做到这一点?我尝试使用 for 循环不起作用,还有另一个问题如何链接到下一个/上一个图像我的网址查找 /gallery/slug_name/image_id.html [注意:我的图像 ID 没有像 1,2、3 那样逐渐增长,例如我在画廊中有 4 张图片,其 ID 为 4、6、7、8]
顺便说一句,我尝试了@murgatroid99 的方式。效果很好!但实际上我必须像这样使用 url
http://localhost:8000/gallery/fourth-gallery/hello?image=2
我想要的是使用分页和网址
http://localhost:8000/gallery/1, http://localhost:8000/gallery/2, http://localhost:8000/gallery/3 Etc