Tuesday, July 28, 2020

Re: Django Pagination paginate dictionary of objects

bro try to send a intended code 

On Tue, Jul 28, 2020 at 5:16 PM Harvindar Singh Garcha <garchaaman19@gmail.com> wrote:
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 &rarr;</a> </div> </div>
 
{% endfor %}
 
</div>
</
div>
</div>
<div class="pagination">
<span class="page-links">
{% if value.has_previous %}
<a href="?page=1">&laquo; 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 &raquo;</
a>
 
{% endif %}
 
</span>
 </
div>
{% else %}
 
<h3>No Results found :(</h3>
{% endif %}
{% endblock content %}





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f2aeb012-8bad-4234-9a4f-635fe1a8698ao%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJ1QTg%3Dq-dCPgexPQmSZ5w8seqh71K-sByLei%2BXkQHs5soj6WA%40mail.gmail.com.

No comments:

Post a Comment