skip to Main Content

Paginator is only working for the first index page but it doesn’t work on the profile page

def profile(request, profile):
    cur_profile = User.objects.get(username=profile)
    user_posts = Posts.objects.filter(creator=cur_profile.id).all().order_by('-id')
    follow_num = cur_profile.following.all().order_by('id')
    following_num = cur_profile.followers.all().order_by('id')
    paginator2 = Paginator(user_posts, 5)
    page_numberr = request.GET.get(paginator2)
    page_objj = paginator2.get_page(page_numberr)
    return render(request, "network/profile.html", {
        "profile": profile,
        "cur_profile": cur_profile,
        "user_posts": user_posts,
        "follow_num": follow_num.count(),
        "following_num": following_num.count(),
        "page_objj": page_objj
    })

my html is this

<div class="pagination">
    <span class="step-links">
        {% if page_objj.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <a href="?page={{ page_objj.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ page_objj.number }} of {{ page_objj.paginator.num_pages }}.
        </span>

        {% if page_objj.has_next %}
            <a href="?page={{ page_objj.next_page_number }}">next</a>
            <a href="?page={{ page_objj.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>

My first page had the same variable names so i changed them but it didn’t work

2

Answers


  1. Chosen as BEST ANSWER

    It worked when I made all html pages extend from an initial one.


  2. def profile(request, profile):
        cur_profile = User.objects.get(username=profile)
        user_posts = Posts.objects.filter(creator=cur_profile.id).all().order_by('-id')
        follow_num = cur_profile.following.all().order_by('id')
        following_num = cur_profile.followers.all().order_by('id')
        paginator2 = Paginator(user_posts, 5)
        page_numberr = request.GET.get('page')
        page_objj = paginator2.get_page(page_numberr)
        return render(request, "network/profile.html", {
            "profile": profile,
            "cur_profile": cur_profile,
            "user_posts": user_posts,
            "follow_num": follow_num.count(),
            "following_num": following_num.count(),
            "page_objj": page_objj
        })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search