0
這是我的看法巫工程確定:Django的REST的架構,如何分頁
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from rest_framework import serializers
from gamerauntsia.berriak.serializers import BerriaSerializer
from rest_framework.response import Response
import json
class JSONResponse(HttpResponse):
"""
An HttpResponse that renders its content into JSON.
"""
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)
@csrf_exempt
def app_berria_list(request):
if request.method == 'GET':
berriak = Berria.objects.all()
serializer = BerriaSerializer(berriak, many=True)
return JSONResponse(serializer.data)
我想通過5項分頁響應,所以我試圖在settings.py添加此:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 5
}
我需要改變一些東西才能使它工作嗎? 在此先感謝
I'll與視圖集中去......那太棒了!我不知道他們:D –