django cbv JsonResponse分页

时间:2017-07-24 16:55:59

标签: python django pagination

我有一些分页的麻烦。 需要分页网址如/api/cards?from=10&to=50和40 json(从10到40)。 我尝试使用django paginator,但它没有用。

views.py

from django.views.generic import View
from django.http import JsonResponse
from cards.models import Card


class Api(View):
    def get(self, request):
        paginate_by = 10
        cards = Card.objects.all()
        response_cards = []
        for card in cards:
            card_dict = {'url': card.url,
                         'image': card.image.path,
                         'type': card.type,
                         'preview': card.preview,
                         'category': card.category.title,
                         'title': card.title,
                         'has_button': card.has_button,
                         'button_text': card.button_text,
                         'button_url': card.button_url}
            response_cards.append(card_dict)
        return JsonResponse(response_cards, safe=False)

urls.py

from django.conf.urls import url
from python_ru.views import Api
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api', Api.as_view()),
]

0 个答案:

没有答案