From ac6fdef17df2f4606b41aca9f1ed582f8d9990c9 Mon Sep 17 00:00:00 2001 From: kyo Date: Sat, 29 Jun 2024 09:30:53 +0800 Subject: [PATCH 1/2] chore: Update URL patterns for post app --- DjangoTutorial/urls.py | 2 +- post/urls.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/DjangoTutorial/urls.py b/DjangoTutorial/urls.py index 058e038..8b833b5 100644 --- a/DjangoTutorial/urls.py +++ b/DjangoTutorial/urls.py @@ -20,5 +20,5 @@ urlpatterns = [ path('admin/', admin.site.urls), - path('post/', include('post.urls')), + path('', include('post.urls')), ] diff --git a/post/urls.py b/post/urls.py index d424010..f90277c 100644 --- a/post/urls.py +++ b/post/urls.py @@ -1,7 +1,8 @@ from django.urls import path -from post.views import hello_world +from post import views urlpatterns = [ - path('hello/', hello_world), + path('hello/', views.hello_world), + path('posts/', views.get_posts), ] From 3f70a7c7e9f66d2bc2fc03ec3578fdd16a358ba6 Mon Sep 17 00:00:00 2001 From: kyo Date: Sat, 29 Jun 2024 09:32:31 +0800 Subject: [PATCH 2/2] feat: Add API endpoint to retrieve posts --- post/views.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/post/views.py b/post/views.py index 41f68cd..ab72333 100644 --- a/post/views.py +++ b/post/views.py @@ -1,5 +1,16 @@ from django.http import JsonResponse +from rest_framework.decorators import api_view +from rest_framework.response import Response + +from post.models import Post def hello_world(request): return JsonResponse({'message': 'Hello, world!'}) + + +@api_view(['GET']) +def get_posts(request): + posts = Post.objects.all() + serializer = PostSerializer(posts, many=True) + return Response(serializer.data)