Jquery code for Django REST framework pagination

时光总嘲笑我的痴心妄想 提交于 2019-12-13 08:47:02

问题


I have this json with pagination from Django REST framework but I don't know how to use it.

{"count": 18, "next": "http://127.0.0.1:8000/ajax/list/?page=6&format=json", 
 "previous": "http://127.0.0.1:8000/ajax/list/?page=4&format=json", 
"results": [{"subject": "fd", "date": "2014-06-20", "time_start": "3:36 AM"},
 {"subject": "fdf", "date": "2014-06-14", "time_start": "3:38 AM"}]}

http url

http://127.0.0.1:8000/ajax/list/?page=5&format=json

django urls.py

url(r'^ajax/list/$', AuthorListAll1.as_view(), name='ajax_list'),

http url

http://127.0.0.1:8000/ajax/list/

I get this http://imgur.com/fMlyXDN

in django view, is template_name = 'authorListAjax.html' work? why do I get this http://imgur.com/fMlyXDN

class AccountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Author
        fields = ('subject', 'date', 'time_start')  

class AuthorListAll1(ListAPIView):
    template_name = 'authorListAjax.html'
    queryset = Author.objects.all()
    serializer_class = AccountSerializer
    paginate_by = 2
    paginate_by_param = 'page_size'
    max_paginate_by = 100

回答1:


You need to define proper renderer classes, you can do it by putting this in your settings:

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.TemplateHTMLRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    )
}

or in your view class, by adding the property:

renderer_classes = (JSONRenderer, TemplateHTMLRenderer, BrowsableAPIRenderer)


来源:https://stackoverflow.com/questions/24561821/jquery-code-for-django-rest-framework-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!