0

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?

Hassan Baig
  • 15,055
  • 27
  • 102
  • 205

3 Answers3

0

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

doniyor
  • 36,596
  • 57
  • 175
  • 260
0

If you are using Django Pagination to get the paginated queryset, you can access the current page number using Page.number:

The 1-based page number for this page.

If you have your object_list in the page, you can include the hyperlink on the first page only (and outside the for loop):

{% if object_list.number == 1 %}
    your hyperlink goes here
{% endif %}
AKS
  • 18,983
  • 3
  • 43
  • 54
0
{{ page_obj.start_index|add:forloop.counter0 }}
Nids Barthwal
  • 2,205
  • 20
  • 12
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Mar 22 '17 at 08:48