Jerome Kambafwile
Jerome Kambafwile

Reputation: 35

Django Pagination with Titles Instead of Numbers

I have a lecture view which I have paginated to 1 lecture per page. I have created a separate list of all the lectures which I want to use as a navigation side bar to the different lectures. I want that side bar to display the lecture titles as links and not numbers.

<ul class="">
  {% for pg in lecture_single.paginator.page_range %}
    {% for lecture_x in lecture_single %}
      <li>
        <a href="?page={{ pg }}" class="btn">{{ lecture.title }}</a>
      </li>
    {% endfor %}
  {% endfor %}
</ul>

I tried using a for loops which did work in creating the links but all the lectures where being presented with the same title as opposed to a specific title for a different page

Upvotes: 2

Views: 46

Answers (1)

Lajos Arpad
Lajos Arpad

Reputation: 76414

lecture is the current lecture, the one whose page you are looking at and therefore lecture.title is the title of your current lecture and, since you display this for all your page links, it is actually the expected behavior to see the same title all over the place.

Since you wanted to display the title of each and every lecture to the corresponding link instead of displaying the current one for all, you will need:

<ul class="">
  {% for pg in lecture_single.paginator.page_range %}
    {% for lecture_x in lecture_single %}
      <li>
        <a href="?page={{ pg }}" class="btn">{{ lecture_x.title }}</a>
      </li>
    {% endfor %}
  {% endfor %}
</ul>

The following are my models

class Course(models.Model):
    level = models.ForeignKey(
        Level, on_delete=models.CASCADE, related_name="courses", 
    default=1
    )
    title = models.CharField(max_length=50, default="")
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    instructor_name = models.ForeignKey(
        Instructor, on_delete=models.CASCADE, 
        related_name="instructor"
    )
    thumbnail = models.ImageField(
        upload_to="learn_course_thumbnails", blank=True, null=True
    )
    recommended = models.BooleanField(default=False)
    category = models.ForeignKey(
        "Category", on_delete=models.CASCADE,         
    related_name="courses"
    )
    ready = models.BooleanField(default=False)
    course_slug = models.CharField(
        max_length=1000, null=True, blank=True, unique=True        
    )
    started = models.BooleanField(default=False)
    finished = models.BooleanField(default=False)



def save(self, *args, **kwargs):
    if not self.course_slug:
        self.course_slug = slugify(self.title)
        while 
Course.objects.filter(course_slug=self.course_slug).exists():
            random_string = 
''.join(random.choice(string.ascii_letters + string.digits) for 
_ in range(3))
            self.course_slug = slugify(self.title + "-" + 
random_string)
    return super().save(*args, **kwargs)




def delete(self, *args, **kwargs):
    self.thumbnail.delete()
    return super().delete(*args, **kwargs)


def __str__(self):
    return self.title

class Lecture(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    content = HTMLField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    course = models.ForeignKey(
        Course, on_delete=models.CASCADE, related_name="lectures"
    )
    lecture_slug = models.CharField(max_length=1000, null=True, 
        blank=True, unique=True)
    completed = models.BooleanField(default=False)


    def save(self, *args, **kwargs):
        if not self.lecture_slug:
            self.lecture_slug = slugify(f"{self.title}")
        return super().save(*args, **kwargs)

    def __str__(self):
        return self.title

And this is my view for the lectures

def all_lectures(request, course_slug):
    course = get_object_or_404(Course, course_slug=course_slug)
    all_lectures_list = Lecture.objects.filter(course=course)

    paginator_lecture = Paginator(all_lectures_list, 1)
    page = request.GET.get("page")
    lecture_single = paginator_lecture.get_page(page)



    context = {
        "course": course,
        "all_lectures_list": all_lectures_list,
        "lecture_single": lecture_single,
    }

    return render(request, "lectures/all_lectures.html", 
    context=context)

Upvotes: 1

Related Questions