0

Here is the relevant code:

models.py

class Blog(models.Model):
    ...
    images = models.ManyToManyField('myapp.Image')

class Image(models.Model):
    ...
    title = models.CharField(max_length=60, blank=True, null=True)
    image = models.FileField(upload_to=content_file_name)
    body = models.TextField(blank=True)
    citation = models.URLField(blank=True)

views.py

def view_post(request, slug):
    ...
    dablog = Blog.objects.get(slug=slug)
    image_list = dablog.images.all()
    paginator = Paginator(image_list, 1)
    ...

In the admin I created one Blog object and two Image objects related to that blog. The code I posted above is only sending the first Image object to paginator and I can't figure out why. I know the rest of my paginator code and the template code work because if i change it to

image_list = Image.objects.all()

I can paginate through both images in the database. Please help this is driving me crazy

1 Answers1

0

You defined paginator to only show one image

def view_post(request, slug):
    ...
    dablog = Blog.objects.get(slug=slug)
    image_list = dablog.images.all()
    paginator = Paginator(image_list, 2)
    ...

This, will show you two images.

J. Ghyllebert
  • 2,009
  • 1
  • 29
  • 35
  • No, I defined paginator to only show one image per page. The problem is that there is no "next" page when there should be – user2458044 Jun 12 '13 at 14:34
  • if you call `len(image_list)` what value do you get? – J. Ghyllebert Jun 12 '13 at 14:40
  • bro you're going the wrong direction. please reread it if you dont understand. especially the part that says image_list = dablog.images.all() only gives me one image when it should two while image_list = Image.objects.all() shows me i can paginate but does not give me the images i need – user2458044 Jun 13 '13 at 08:15
  • My answer my not be right, but as i can't see your images, i just wanted to make sure both images are coupled to your Blog-object. Because if you call `image_list = Image.objects.all()` you get them, so my guess is that one of your images isn't actually linked to your blog. – J. Ghyllebert Jun 13 '13 at 08:29
  • Thank you, that's exactly what I'm thinking but I don't know what the problem in my model is. – user2458044 Jun 13 '13 at 09:30
  • Could you look in the table `myapp_blog_images`, maybe there's only one row there... – J. Ghyllebert Jun 13 '13 at 09:36
  • good call, myapp_blog_images only contains one row: id, image_id, blog_id, all of their values are 1 but myapp_image contains the two rows of values that i'm looking for. what's wrong with the model? its obviously broken but i dont know why – user2458044 Jun 13 '13 at 10:29
  • Something must have gone wrong uploading your image. You could manually enter the data in the database, or you can retry adding it in the admin. In your code is nothing wrong, however. – J. Ghyllebert Jun 13 '13 at 10:38