Skip to content

Commit 49a8c90

Browse files
author
zmrenwu
committed
Step3: 用类视图实现首页 API
1 parent 796491f commit 49a8c90

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ $ git clone https://github.com/HelloGitHub-Team/HelloDjango-REST-framework-tutor
190190
1. [开篇](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/)
191191
2. [django-rest-framework 是什么鬼?](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/91/)
192192
3. [初始化 RESTful API 风格的博客系统](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/92/)
193-
4. [实现博客首页文章列表 API](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/92/)
193+
4. [实现博客首页文章列表 API](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/93/)
194+
5. [用类视图实现首页 API](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/94/)
194195

195196
## 公众号
196197
<p align="center">

blog/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
),
1212
path("categories/<int:pk>/", views.CategoryView.as_view(), name="category"),
1313
path("tags/<int:pk>/", views.TagView.as_view(), name="tag"),
14-
path("api/index/", views.index),
14+
path("api/index/", views.IndexPostListAPIView.as_view()),
1515
]

blog/views.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from django.views.generic import DetailView, ListView
55
from rest_framework import status
66
from rest_framework.decorators import api_view
7+
from rest_framework.generics import ListAPIView
8+
from rest_framework.pagination import PageNumberPagination
9+
from rest_framework.permissions import AllowAny
710
from rest_framework.response import Response
811

912
from pure_pagination.mixins import PaginationMixin
@@ -64,8 +67,8 @@ def get(self, request, *args, **kwargs):
6467
return response
6568

6669

67-
@api_view(http_method_names=["GET"])
68-
def index(request):
69-
post_list = Post.objects.all().order_by("-created_time")
70-
serializer = PostListSerializer(post_list, many=True)
71-
return Response(serializer.data, status=status.HTTP_200_OK)
70+
class IndexPostListAPIView(ListAPIView):
71+
serializer_class = PostListSerializer
72+
queryset = Post.objects.all()
73+
pagination_class = PageNumberPagination
74+
permission_classes = [AllowAny]

blogproject/settings/common.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,9 @@
131131
HAYSTACK_CUSTOM_HIGHLIGHTER = "blog.utils.Highlighter"
132132
# HAYSTACK_DEFAULT_OPERATOR = 'AND'
133133
# HAYSTACK_FUZZY_MIN_SIM = 0.1
134+
135+
# django-rest-framework
136+
# ------------------------------------------------------------------------------
137+
REST_FRAMEWORK = {
138+
"PAGE_SIZE": 10,
139+
}

0 commit comments

Comments
 (0)