0

I made a pagination on API. After that I got a problem. I cant display my datas that I fetched from api. So after implementation of pagination , Displaying is stopped.

pagination.py

from rest_framework.pagination import PageNumberPagination

class SmallPagination(PageNumberPagination):
    page_size =5

List Api

class MeetingList(generics.ListAPIView):
queryset = CreateNewMeeting.objects.all()
pagination_class = SmallPagination
serializer_class = MeetingSerializer
Permission_Classes = [permissions.IsAuthenticatedOrReadOnly]
filter_backends = (SearchFilter, OrderingFilter)
search_fields = ('meeting_name', 'id')

index.html

def MeetingViewSearch(request):
    meeting = "http://127.0.0.1:8000/meetingdetails/?page=1"
    
    read_meeting = requests.get(meeting).json()

    context = {'meetings': read_meeting}     
return render(request, 'index.html', context)

Template

    {% extends 'base.html' %}

{% block content %}
<div class="container pt-5">

<table class="uk-table uk-table-middle uk-table-divider">
    <thead>
    </thead>
    <tbody>

        {% for meeting in meetings %}

        <tr>
            <td>{{meeting.id}}</td>
            <td>{{meeting.meeting_name}}</td>
            <td>{{meeting.meeting_limit}} </td>                         
            <td>{{meeting.meeting_creator}}<br></td>
            <td>{{meeting.meeting_created_date}}<br></td>
        </tr>

        {% endfor %}
        
    </tbody>
</table>
{% endblock %}

I'll be glad If someone could help me ...

Kusursuz
  • 39
  • 7
  • What is the error message, or else what yo dou mean by stop displaying ? – Sami Tahri Feb 21 '22 at 20:02
  • There is no error message. Just not displaying. I cant get datas because I made pagination. – Kusursuz Feb 21 '22 at 20:04
  • what is your request ? What is the output it gives you then (the responses ?) ? – Sami Tahri Feb 21 '22 at 20:06
  • meeting = "http://127.0.0.1:8000/meetingdetails/" read_meeting = requests.get(meeting).json() I tried to fetch data . And displayed in html template. But After pagination , Its stopped to displaying. – Kusursuz Feb 21 '22 at 20:11
  • 1
    Is that two separate app (if so, why not just render the template directly ?)? Can you share your template file ? I guess it might be because drf uses an enveloppe. – Sami Tahri Feb 21 '22 at 20:17
  • I shared . If you have another advise for this , I would like to know it. I would be glad. – Kusursuz Feb 21 '22 at 20:31

1 Answers1

1

As you can see there, this pagination will create by default an enveloppe, which will render you json as : { "count": 1023, "next": "https://api.example.org/accounts/?page=5", "previous": "https://api.example.org/accounts/?page=3", "results": [ … ] }

as you can see, it means that the fetched value is not an array, but actually an object, which holds you're array in the results field. So you might do that :

read_meeting = requests.get(meeting).json()['results']

PS: Also, it looks weird to query internally through HTTP your own service to render the template, unless it's going to be on a separate instance ?

Sami Tahri
  • 1,077
  • 9
  • 19
  • You are my hero! Really Thank you so much ! In additionally , yes I tried to use template but I couldnt do it. How can I use in my api view class? Do you know ? – Kusursuz Feb 21 '22 at 20:43
  • Well, you don't need to call your API to fetch those objects from your database. What ListApiView is doing, is using your define models, fetching them as paginated, and then calling serializers. What you want here is just retrieving your objects : `read_meeting = CreateNewMeeting.objects.all()`, if you want to paginate this, either do it manually (with slicing) or using it with this : https://docs.djangoproject.com/en/4.0/topics/pagination/ – Sami Tahri Feb 21 '22 at 20:52