【问题标题】:Jquery code for Django REST framework paginationDjango REST框架分页的jQuery代码
【发布时间】:2014-08-25 01:21:35
【问题描述】:

我有这个来自 Django REST 框架的带有分页的 json,但我不知道如何使用它。

{"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 网址

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 网址

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

我知道了http://imgur.com/fMlyXDN

在 django 视图中,template_name = 'authorListAjax.html' 有效吗?为什么我会得到这个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

【问题讨论】:

    标签: jquery django django-templates django-rest-framework


    【解决方案1】:

    您需要定义正确的渲染器类,您可以通过将其放入您的设置来做到这一点:

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

    或在您的视图类中,通过添加属性:

    renderer_classes = (JSONRenderer, TemplateHTMLRenderer, BrowsableAPIRenderer)
    

    【讨论】:

    猜你喜欢
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2017-12-28
    • 2014-10-09
    • 2014-08-24
    • 2015-06-05
    • 1970-01-01
    相关资源
    最近更新 更多