user3528314
user3528314

Reputation: 45

Custom pagination in Django is not working properly

I created a pagination in django using a dropdown which lists the pages for the user to select from. When any value is selected a javascript function is called which calls the django backend to fetch the data.

The response object from django includes the page number which was selected so that the same page can be kept selected in HTML (since the page reloads to render the template).

<div>
    Pages :
<select name="pagination" id="pagination_id" onchange="pageChange()">
    {% for i in pages %}
    <option value={{i}} {% if page_selected == i %} selected="selected" {% endif %}> {{i}} </option>
    {% endfor %}
</select>
</div>

However, the dropdown selection is always the first item even though the data in HTML table changes. The value in page_selected is appropriate but the dropdown does not select on the correct page. What am I doing wrong ?

Upvotes: 1

Views: 94

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476740

If you pass the parameter directly from request.GET, then the page_selected is a string.

You should cast it to an integer, with int(…):

context = {
    'pages': pages,
    'page_selected': int(page_selected),
    # …
}

Upvotes: 1

Related Questions