0

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]

BTW I tried @murgatroid99 way. It works great! But actually I then have to use url like this

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

What I want is to use pagination and url like

http://localhost:8000/gallery/1, http://localhost:8000/gallery/2, http://localhost:8000/gallery/3 Etc
themunna
  • 47
  • 1
  • 6

2 Answers2

0

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.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95
  • but then how should I link any of this from other page? Now what I'm doing is a FB style gallery system. first page show all the gallery. Click on a gallery will open a page where all the images of that gallery will be showing. any of this image is linked to the single image page. when its in any image page pagination links should work – themunna Aug 10 '11 at 17:57
0

You should not use all as a variable because there is a built-in function in python. See here http://docs.python.org/library/functions.html#all

You can just use django's pagination to show the images. In your case, pagesize will be 1 as your are showing 1 image object per page. Django pagination has built-in functionality to find where next or previous is available or not.

Ershadul
  • 61
  • 1
  • 4
  • Yes I already did that. Problem is I'm getting url like this[http://localhost:8000/gallery/fourth-gallery/hello?image=2 ] when I'm using pagination. But I need http://localhost:8000/gallery/fourth-gallery/2/ and pagination should work like 2, 3, 4 etc when clicking on next button. – themunna Aug 11 '11 at 05:59