Home > OS >  I can not fetch Datas from Django Rest Frame work Api
I can not fetch Datas from Django Rest Frame work Api

Time:02-22

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 >

<table >
    <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 ...

CodePudding user response:

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 ?

  • Related