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), ] 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)