Permalink
Browse files

Smarter ?page= links using new page_href template tag

  • Loading branch information...
simonw committed Oct 1, 2017
1 parent e65120e commit 74c194d957689badc9a993cc174b18c2b22e1752
Showing with 13 additions and 4 deletions.
  1. +9 −0 blog/templatetags/blog_tags.py
  2. +2 −2 templates/archive_tag.html
  3. +2 −2 templates/search.html
@@ -40,3 +40,12 @@ def comments_list_with_headers(context, comments):
'show_headers': True,
})
return context
@register.simple_tag(takes_context=True)
def page_href(context, page):
query_dict = context['request'].GET.copy()
if page == 1 and 'page' in query_dict:
del query_dict['page']
query_dict['page'] = page
return '?' + query_dict.urlencode()
@@ -12,13 +12,13 @@ <h2>{{ total }} item{{ total|pluralize }} tagged “{{ tags|join:"” and “"|e
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}">&laquo; previous</a>
<a href="{% page_href page.previous_page_number %}">&laquo; previous</a>
{% endif %}
<span class="current">
Page {{ page.number }} / {{ page.paginator.num_pages }}
</span>
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}">next &raquo;</a>
<a href="{% page_href page.next_page_number %}">next &raquo;</a>
{% endif %}
</span>
</div>
View
@@ -19,13 +19,13 @@ <h2>Search{% if q %} for “{{ q }}”{% endif %}</h2>
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
<a href="?q={{ q|urlencode }}&amp;page={{ page.previous_page_number }}">&laquo; previous</a>
<a href="{% page_href page.previous_page_number %}">&laquo; previous</a>
{% endif %}
<span class="current">
Page {{ page.number }} / {{ page.paginator.num_pages }}
</span>
{% if page.has_next %}
<a href="?q={{ q|urlencode }}&amp;page={{ page.next_page_number }}">next &raquo;</a>
<a href="{% page_href page.next_page_number %}">next &raquo;</a>
{% endif %}
</span>
</div>

0 comments on commit 74c194d

Please sign in to comment.