Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #171 from walison17/fix-pagination-links
Browse files Browse the repository at this point in the history
Fix pagination links
  • Loading branch information
vmesel authored Oct 3, 2019
2 parents 166a973 + 97b917d commit b7ee491
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyjobs/core/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% load static %}
{% load humanize %}
{% load widget_tweaks %}
{% load core_tags %}
{%block title%} {{WEBSITE_NAME}} - A Central de Jobs {{WEBSITE_WORKING_LANGUAGE}}
{%endblock%}
{%block main%}
Expand Down Expand Up @@ -132,7 +133,7 @@ <h6 class="text-muted card-subtitle mb-2">Postada: {{job.created_at|naturalday}}
<ul class="pagination">
{% for page in pages %}
<li class="page-item">
<a class="page-link" href="{% url 'index' %}?page={{page}}{% if 'search' in request.GET %}&search={{ request.GET.search }}{% endif %}">{{ page }}</a>
<a class="page-link" href="{% url 'index' %}?{% merge_query_params page=page %}">{{ page }}</a>
</li>
{% endfor %}
</ul>
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions pyjobs/core/templatetags/core_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django import template

register = template.Library()


@register.simple_tag(takes_context=True)
def merge_query_params(context, **kwargs):
query_params = context["request"].GET.copy()

for k, v in kwargs.items():
query_params[k] = v

return query_params.urlencode()
37 changes: 37 additions & 0 deletions pyjobs/core/tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.test import SimpleTestCase, RequestFactory
from django.template import Context, Template


class TestMergeQueryParamsTag(SimpleTestCase):
def setUp(self):
self.rf = RequestFactory()

def test_add_param(self):
request = self.rf.get("/", {"description": "descricao", "salary_range": 1})
context = Context({"request": request})
template = Template(
"{% load core_tags %}"
'<a class="page-link" href="/?{% merge_query_params page=1 %}">1</a>'
)
rendered_template = template.render(context)

self.assertInHTML(
'<a class="page-link" href="/?description=descricao&salary_range=1&page=1">1</a>',
rendered_template,
)

def test_replace_param(self):
request = self.rf.get(
"/", {"description": "descricao", "salary_range": 1, "page": 2}
)
context = Context({"request": request})
template = Template(
"{% load core_tags %}"
'<a class="page-link" href="/?{% merge_query_params page=10 %}">1</a>'
)
rendered_template = template.render(context)

self.assertInHTML(
'<a class="page-link" href="/?description=descricao&salary_range=1&page=10">1</a>',
rendered_template,
)

0 comments on commit b7ee491

Please sign in to comment.