简体   繁体   English

django 1.3 中模板中的自定义分页问题

[英]a custom pagination problem in template in django 1.3

I'm making a gallery where i have two models.我正在制作一个有两个模型的画廊。 like below像下面

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

Now I have a view which will serve a single image page.现在我有一个视图,它将提供一个图像页面。 I'm getting the images like this我得到这样的图像

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))

By "image" I'm getting that single image.通过“图像”,我得到了那个单一的图像。 by "all" I'm getting all the images which Probably need in the view for displaying pagination.通过“全部”,我得到了视图中可能需要用于显示分页的所有图像。 Pagination means only a next and previous button.分页仅意味着下一个和上一个按钮。

Now if this single page is first page previous link should not show similarly if last page next button should not show.现在,如果此单页是第一页,则如果最后一页下一页按钮不应显示,则上一个链接不应显示类似。 Other then that the both link should display.除此之外,两个链接都应该显示。

My question is how to do it in template?我的问题是如何在模板中做到这一点? I tried using for loop not working, also another question how to link to next/previous image my urls look for /gallery/slug_name/image_id.html [note: my image id isn't growing gradually like 1,2, 3, example I have 4 image in a gallery which ids are 4, 6, 7, 8]我尝试使用 for 循环不起作用,还有另一个问题如何链接到下一个/上一个图像我的网址查找 /gallery/slug_name/image_id.html [注意:我的图像 ID 没有像 1,2、3 那样逐渐增长,例如我在画廊中有 4 张图片,其 ID 为 4、6、7、8]

BTW I tried @murgatroid99 way.顺便说一句,我尝试了@murgatroid99 的方式。 It works great!效果很好! But actually I then have to use url like this但实际上我必须像这样使用 url

http://localhost:8000/gallery/fourth-gallery/hello?image=2

What I want is to use pagination and url like我想要的是使用分页和 url 之类的

http://localhost:8000/gallery/1, http://localhost:8000/gallery/2, http://localhost:8000/gallery/3 Etc

The best way would probably be to have the view generate the pages based on information in the request and then just have the template render the page returned by the view.最好的方法可能是让视图根据请求中的信息生成页面,然后让模板呈现视图返回的页面。 A good explanation and example can be found in the Django documentation .可以在Django 文档中找到一个很好的解释和示例。

You should not use all as a variable because there is a built-in function in python.你不应该使用 all 作为变量,因为在 python 中有一个内置的 function。 See here http://docs.python.org/library/functions.html#all请参阅此处http://docs.python.org/library/functions.html#all

You can just use django's pagination to show the images.您可以只使用 django 的分页来显示图像。 In your case, pagesize will be 1 as your are showing 1 image object per page.在您的情况下,页面大小将为 1,因为您每页显示 1 张图像 object。 Django pagination has built-in functionality to find where next or previous is available or not. Django 分页具有查找下一个或上一个可用或不可用的内置功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM