I am new to Django, and trying to paginate a dictionary objects, I am 
calling stackoverflow API where I want the result to get paginated. However 
I am unable to so and not getting what's wrong with my code. 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
>
>  })
>
>>
>>

My template file


{%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 %}







*Can someone tell me what's wrong with my code, I am unable to get a 
paginated result, everything is coming all together on single page.*




-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/683ad3c2-16d6-4eea-9074-5336251165aeo%40googlegroups.com.

Reply via email to