我无法从 Django Rest Framework 获取数据 Api
I can not fetch Datas from Django Rest Frame work Api
我在 API 上做了一个分页。之后我遇到了问题。我无法显示从 api 获取的数据。所以执行分页后,显示就停止了。
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 %}
如果有人能帮助我,我会很高兴...
如您所见 there,此分页将默认创建一个信封,它将 json 呈现为:
{
“计数”:1023,
"下一步": "https://api.example.org/accounts/?page=5",
"previous": "https://api.example.org/accounts/?page=3",
“结果”: [
……
]
}
如你所见,这意味着获取的值不是一个数组,而是一个对象,它持有你在 results
字段中的数组。所以你可以这样做 :
read_meeting = requests.get(meeting).json()['results']
PS:此外,通过 HTTP 内部查询您自己的服务来呈现模板看起来很奇怪,除非它要在一个单独的实例上?
我在 API 上做了一个分页。之后我遇到了问题。我无法显示从 api 获取的数据。所以执行分页后,显示就停止了。
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 %}
如果有人能帮助我,我会很高兴...
如您所见 there,此分页将默认创建一个信封,它将 json 呈现为: { “计数”:1023, "下一步": "https://api.example.org/accounts/?page=5", "previous": "https://api.example.org/accounts/?page=3", “结果”: [ …… ] }
如你所见,这意味着获取的值不是一个数组,而是一个对象,它持有你在 results
字段中的数组。所以你可以这样做 :
read_meeting = requests.get(meeting).json()['results']
PS:此外,通过 HTTP 内部查询您自己的服务来呈现模板看起来很奇怪,除非它要在一个单独的实例上?