Skip to content

Commit

Permalink
Merge pull request #96 from SADiLaR/feature/documents-list-page
Browse files Browse the repository at this point in the history
Simplify handling of url parameters with pagination
  • Loading branch information
friedelwolff authored Jul 23, 2024
2 parents 3217164 + 6e39882 commit 9f1b97b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def institution_detail(request, institution_id):
def documents(request):
template = "app/documents.html"

url_params = {}
subject_id = request.GET.get("subject")
language_id = request.GET.get("language")
institution_id = request.GET.get("institution")
Expand All @@ -182,10 +183,13 @@ def documents(request):

if subject_id:
documents = documents.filter(subjects__id=subject_id)
url_params["subject"] = subject_id
if language_id:
documents = documents.filter(languages__id=language_id)
url_params["language"] = language_id
if institution_id:
documents = documents.filter(institution__id=institution_id)
url_params["institution"] = institution_id

paginator = Paginator(documents, 10)

Expand Down Expand Up @@ -220,6 +224,7 @@ def documents(request):
"current_page": "documents",
"page_obj": page_obj,
"documents": document_data,
"url_params": urlencode(url_params),
"subjects": subjects,
"languages": languages,
"institutions": institutions,
Expand Down
10 changes: 5 additions & 5 deletions app/templates/app/documents.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ <h3>
<div class="pagination-wrapper">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?{% if request.GET.subject %}subject={{ request.GET.subject }}&{% endif %}{% if request.GET.language %}language={{ request.GET.language }}&{% endif %}{% if request.GET.institution %}institution={{ request.GET.institution }}&{% endif %}page=1">&laquo; {% trans "First" %}</a></li>
<li class="page-item"><a class="page-link" href="?{% if request.GET.subject %}subject={{ request.GET.subject }}&{% endif %}{% if request.GET.language %}language={{ request.GET.language }}&{% endif %}{% if request.GET.institution %}institution={{ request.GET.institution }}&{% endif %}page={{ page_obj.previous_page_number }}">&lsaquo; {% trans "Previous" %}</a></li>
<li class="page-item"><a class="page-link" href="?{{ url_params }}&page=1">&laquo; {% trans "First" %}</a></li>
<li class="page-item"><a class="page-link" href="?{{ url_params }}&page={{ page_obj.previous_page_number }}">&lsaquo; {% trans "Previous" %}</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">&laquo; {% trans "First" %}</a></li>
<li class="page-item disabled"><a class="page-link" href="#">&lsaquo; {% trans "Previous" %}</a></li>
Expand All @@ -96,13 +96,13 @@ <h3>
{% if page_obj.number == num %}
<li class="page-item active"><a class="page-link" href="#">{{ num }}</a></li>
{% elif num > page_obj.number|add:-3 and num < page_obj.number|add:3 %}
<li class="page-item"><a class="page-link" href="?{% if request.GET.subject %}subject={{ request.GET.subject }}&{% endif %}{% if request.GET.language %}language={{ request.GET.language }}&{% endif %}{% if request.GET.institution %}institution={{ request.GET.institution }}&{% endif %}page={{ num }}">{{ num }}</a></li>
<li class="page-item"><a class="page-link" href="?{{ url_params }}&page={{ num }}">{{ num }}</a></li>
{% endif %}
{% endfor %}

{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?{% if request.GET.subject %}subject={{ request.GET.subject }}&{% endif %}{% if request.GET.language %}language={{ request.GET.language }}&{% endif %}{% if request.GET.institution %}institution={{ request.GET.institution }}&{% endif %}page={{ page_obj.next_page_number }}">{% trans "Next" %} &rsaquo;</a></li>
<li class="page-item"><a class="page-link" href="?{% if request.GET.subject %}subject={{ request.GET.subject }}&{% endif %}{% if request.GET.language %}language={{ request.GET.language }}&{% endif %}{% if request.GET.institution %}institution={{ request.GET.institution }}&{% endif %}page={{ page_obj.paginator.num_pages }}">{% trans "Last" %} &raquo;</a></li>
<li class="page-item"><a class="page-link" href="?{{ url_params }}&page={{ page_obj.next_page_number }}">{% trans "Next" %} &rsaquo;</a></li>
<li class="page-item"><a class="page-link" href="?{{ url_params }}&page={{ page_obj.paginator.num_pages }}">{% trans "Last" %} &raquo;</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">{% trans "Next" %} &rsaquo;</a></li>
<li class="page-item disabled"><a class="page-link" href="#">{% trans "Last" %} &raquo;</a></li>
Expand Down

0 comments on commit 9f1b97b

Please sign in to comment.