Nanthagopal
Nanthagopal

Reputation: 229

pagination does not work in Django template page

I was trying to include pagination in template page. but the template syntax error was encountered when executing the project.

it says : Could not parse the remainder: '==i' from 'posts.number==i'

I am very frustrated about this.

views.py

def posts(request):
    posts = Post.objects.filter(active=True)
    myFilter = PostFilter(request.GET, queryset=posts)
    posts = myFilter.qs

    page = request.GET.get('page')
    paginator = Paginator(posts, 3)
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)

    context = {'posts': posts, 'myFilter': myFilter}
    return render(request, 'base/posts.html', context)

posts.html

<div class="row">
        {%if posts.has_other_pages%}
        <ul class="pagination">
            {%for i in posts.paginator.page_range%}
            {%if posts.number==i%}
            <li class="page-item"><a class="active page-link">{{i}}</a></li>
            {%else%}
            <li class="page-item"><a href="?page={{i}}" class="page-link">{{i}}</a></li>
            {%endif%}
            {%endfor%}
        </ul>
        {%endif%}
    </div>

Upvotes: 0

Views: 591

Answers (1)

user14285208
user14285208

Reputation:

Please add space between posts.number and i. I suggest for things like this: {%if posts.number == i%} instead of {%if posts.number==i%}

<div class="row">
  {%if posts.has_other_pages%}
  <ul class="pagination">
    {%for i in posts.paginator.page_range%}
    {%if posts.number == i%}
    <li class="page-item"><a class="active page-link">{{i}}</a></li>
    {%else%}
    <li class="page-item"><a href="?page={{i}}" class="page-link">{{i}}</a></li>
    {%endif%}
    {%endfor%}
  </ul>
  {%endif%}
</div>

Upvotes: 1

Related Questions