Pagination for thing_detail view


#1

Hi There,

I would love to be able to paginate each of the “things” entries when in thing_detail.html view, for example “prev post | next post” etc. I can paginate the index view using the code from django - https://docs.djangoproject.com/en/1.9/topics/pagination/#using-paginator-in-a-view.

WP php code is as such -

<div class="project-links">
	<?php next_post_link('%link', 'next'); ?>
	<?php previous_post_link('%link', 'previous'); ?>
</div>

It’s not covered in the book, that I know of and I have been looking everywhere (google, stackoverflow, django project), for days to find a solution with no luck. Can you please point me in the right direction?

Cheers :)


#2

I’ll try to help!

So first, open up the Django docs. You need to update the view for your template so it sends along the items in paginated format — that’s the def listing(request): code block in the docs, and note that it’s passing along contacts to the template, which is created via the paginator helper.

Then in the code block for list.html, you can see the template code, which is pretty much the equivalent of the WP code you pasted in — the pagination code creates the next and previous buttons.

Does that help explain the docs better?


#3

Hi again,

Sorry, I probably wasn’t clear in my question. I can paginate pages from the post_list.html view as per the django docs, works great. I want to be able to navigate each of the individual items or posts using next or previous buttons. So when viewing post 2, I can click next to go to the next post or previous to the previous.

My view for the blog post list is:

# blog index
def post_list(request):

post_articles = Post.objects.all()
paginator = Paginator(post_articles, 12)
page = request.GET.get('page')

try:
    post_articles = paginator.page(page)
except PageNotAnInteger:
    post_articles = paginator.page(1)
except EmptyPage:
    post_articles = paginator.page(paginator.num_pages)

return render(request, 'post_list.html', {'post_articles': post_articles})

My view for the individual post:

# blog view single
def post_detail(request, slug):

    post = Post.objects.get(slug=slug)

    return render(request, 'post/post_detail.html', {
        'post': post,
    })

I also would love to know how you approach adding in Meta content to the header for each of the pages and posts ie. meta=“description”, meta=“title”. I have looked at some django plugins but they seem overly complicated and don’t work with the latest django version.

Lastly, how do you add in dynamic fields for say a contact or about page, where you can modify the fields via the admin. I have setup flatpages but they are not really full featured enough.

Sorry there so many questions, I will add I have researched a lot, I just can’t seem to find straight forward enough solutions for what I want, which in the end is a Wordpress replacement.

Cheers for your help, really appreciate it!


#4

Ack — I’m going to have to punt on the question then, I have no idea how to access the pagination/next/previous object from an individual object page. :( I’ll post this on Twitter and hopefully someone else will know?

EDIT: Wait, we might be overcomplicating things by involving paginator. If you simply want to link to previous/next objects, every object has an ID, so could you just link to the next/previous numbered ID? So if you’re on the page for id=6, you’d link to id=7?

Re: meta content, just stick the meta tags in your base template and have {% block name %}{% endblock %} where you’d want to override info in individual pages.

Unfortunately I don’t know how to do dynamic fields (if I get what you mean). :( A lot of these questions would be great for Stack Overflow, sorry I can’t help more!


#5

Hey thanks, I will do some research / googling :) re: pulling in post IDs, I’m sure someone out there has done it before!

Thanks again.


#6

Hi there,

If anyone wants the answer on how to paginate the pre/next post object for an individual view page (eg. single post), this is the answer, thanks to Alasdair from stackoverflow - https://stackoverflow.com/questions/38762146/django-single-post-view-prev-next-links

views.py

from django.shortcuts import get_object_or_404

def film_detail(request, slug):

film = get_object_or_404(Film, slug=slug)
try:
    next_film = film.get_next_by_date_published()
except Film.DoesNotExist:
    next_film = None

try:
    previous_film = film.get_previous_by_date_published()
except Film.DoesNotExist:
    previous_film = None

return render(request, 'film/film_detail.html', {
    'film': film,
    'next_film': next_film,
    'previous_film': previous_film
})

film_detail.html

{% if next_film %}
    <a href="{% url 'film_detail' next_film.slug %}">Next</a>
{% else %}
    This is the last film!
{% endif %}

Gotta love the internet, stoked!! :)


#7

Yay thanks for sharing the answer!