I am new to Django, and trying to paginate a dictionary objects,
However I
am unable to so and not getting what's wrong with my code,
Its still printing whole data on a single page. Above I have posted my View along with template code.
class Search(ListView):
paginate_by = 5
def get(self, request):
if 'search' in request.GET:
search = request.GET['search']
url = 'https://api.stackexchange.com/2.2/search/advanced?&site=stackoverflow'
params = dict(item.split("=") for item in search.split(","))
req = PreparedRequest()
req.prepare_url(url, params)
stackoverflow_url = req.url
response = requests.get(stackoverflow_url)
data = response.json()
#Data will be like this
# data={{'tag':'python','question':'some question'},{'tag':'java','question':'some question'}}
# n here is 2
paginator = Paginator(list(data), 5)
page_number = request.GET.get('search')
page_obj = paginator.get_page(page_number)
return render(request, 'stackoverflow.html', {
'data': data,
'page_obj': page_obj
})
{%if data %}
<div class="container">
<div class="row"> <!-- Blog Entries Column -->
<div class="col-md-8 mt-3 left">
{% for key,value in data.items %}
<div class="card mb-4">
<div class="card-body">
<h2 class="card-title">{{ value.title }}</h2>
<p class="card-text text-muted h6">{{ value.creation_date }} </p>
<p class="card-text text-muted h6"> Asked By</p> <a href="{{value.owner_link}}">{{value.display_name }} </a>
<p class="card-text"></p> <a href="{{value.link}}" class="btn btn-primary">Read More →</a> </div> </div>
{% endfor %}
</div>
</div>
</div>
<div class="pagination">
<span class="page-links">
{% if value.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="page-current"> Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
<a href="?page={{ page_obj.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
{% else %}
<h3>No Results found :(</h3>
{% endif %}
{% endblock content %}