Skip to content

Commit 796491f

Browse files
author
zmrenwu
committed
Step2: 实现博客首页文章列表 API
1 parent b238cce commit 796491f

File tree

6 files changed

+62
-9
lines changed

6 files changed

+62
-9
lines changed

.idea/HelloDjango-rest-framework-tutorial.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ $ 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/)
193194

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

blog/serializers.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from django.contrib.auth.models import User
2+
from rest_framework import serializers
3+
4+
from .models import Category, Post
5+
6+
7+
class CategorySerializer(serializers.ModelSerializer):
8+
class Meta:
9+
model = Category
10+
fields = [
11+
"id",
12+
"name",
13+
]
14+
15+
16+
class UserSerializer(serializers.ModelSerializer):
17+
class Meta:
18+
model = User
19+
fields = [
20+
"id",
21+
"username",
22+
]
23+
24+
25+
class PostListSerializer(serializers.ModelSerializer):
26+
category = CategorySerializer()
27+
author = UserSerializer()
28+
29+
class Meta:
30+
model = Post
31+
fields = [
32+
"id",
33+
"title",
34+
"created_time",
35+
"excerpt",
36+
"category",
37+
"author",
38+
"views",
39+
]

blog/urls.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
from . import views
44

5-
app_name = 'blog'
5+
app_name = "blog"
66
urlpatterns = [
7-
path('', views.IndexView.as_view(), name='index'),
8-
path('posts/<int:pk>/', views.PostDetailView.as_view(), name='detail'),
9-
path('archives/<int:year>/<int:month>/', views.ArchiveView.as_view(), name='archive'),
10-
path('categories/<int:pk>/', views.CategoryView.as_view(), name='category'),
11-
path('tags/<int:pk>/', views.TagView.as_view(), name='tag'),
12-
# path('search/', views.search, name='search'),
7+
path("", views.IndexView.as_view(), name="index"),
8+
path("posts/<int:pk>/", views.PostDetailView.as_view(), name="detail"),
9+
path(
10+
"archives/<int:year>/<int:month>/", views.ArchiveView.as_view(), name="archive"
11+
),
12+
path("categories/<int:pk>/", views.CategoryView.as_view(), name="category"),
13+
path("tags/<int:pk>/", views.TagView.as_view(), name="tag"),
14+
path("api/index/", views.index),
1315
]

blog/views.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
from django.db.models import Q
33
from django.shortcuts import get_object_or_404, redirect, render
44
from django.views.generic import DetailView, ListView
5+
from rest_framework import status
6+
from rest_framework.decorators import api_view
7+
from rest_framework.response import Response
58

69
from pure_pagination.mixins import PaginationMixin
710

811
from .models import Category, Post, Tag
12+
from .serializers import PostListSerializer
913

1014

1115
class IndexView(PaginationMixin, ListView):
@@ -58,3 +62,10 @@ def get(self, request, *args, **kwargs):
5862

5963
# 视图必须返回一个 HttpResponse 对象
6064
return response
65+
66+
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)

0 commit comments

Comments
 (0)