Search code examples
djangodjango-templatesdjango-pagination

Manipulating forloop counter in Django template


I have a paginated Django template, where I loop through elements of a list and display them. I want a hyperlink to display at the very top of the list, and solely on the first page, not subsequent ones.

I'm currently enclosing that hyperlink within {% if forloop.counter == 1 %}{% endif %}.

However, this ouputs the hyperlink at the start of every page. How do I limit it solely to the first page?


Solution

  • something like this?

    {% if forloop.first and items.number == 1 %}{% endif %}
    

    Or

    {% if forloop.first and not items.has_previous %}
    

    "items" must be replaced with paginated items you render to template