urls.py and views.py are setup for browsable API

master
Jason Zhu 2020-10-21 15:35:55 +11:00
parent 762296b276
commit 7310f1818c
6 changed files with 82 additions and 7 deletions

View File

@ -2,3 +2,15 @@
This is a practice repository following [Test Driven Development of a Django RESTful API](https://realpython.com/test-driven-development-of-a-django-restful-api/)
## RESTful Structure
In a RESTful API, endpoints (URLs) define the structure of the API and how end users access data from our application using the HTTP methods - GET, POST, PUT, DELETE. Endpoints should be logically organized around collections and elements, both of which are resources.
In our case, we have one single resource, puppies, so we will use the following URLS - /puppies/ and /puppies/<id> for collections and elements, respectively
## Routes and TDD
Process for development:
1. Create skeleton code that doomed to fail
2. Add a unit test for failure
3. Update the code to make it pass the test.

View File

@ -0,0 +1,10 @@
import json
from rest_framework import status
from django.test import TestCase, Client
from django.urls import reverse
from ..models import Puppy
from ..serializers import PuppySerializer
# initialize the APIClient app
client = Client()

View File

@ -0,0 +1,16 @@
from django.conf.urls import url
from . import views
urlpatterns = [
# suppose this is the django1's code style, check django REST framework for django3.1
url(
r'^api/v1/puppies/(?P<pk>[0-9]+)$',
views.get_delete_update_puppy,
name='get_delete_update_puppy'
),
url(
r'^api/v1/puppies/$',
views.get_post_puppies,
name='get_post_puppies'
)
]

View File

@ -1,3 +1,34 @@
from django.shortcuts import render
from django.test.client import RequestFactory
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
# Create your views here.
from .models import Puppy
from .serializers import PuppySerializer
@api_view(['GET', 'DELETE', 'PUT'])
def get_delete_update_puppy(request, pk):
try:
puppy = Puppy.objects.get(pk=pk)
except Puppy.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
# get details of a single puppy
if request.method == 'GET':
return Response({})
# delete a single puppy
elif request.method == 'DELETE':
return Response({})
# update details of a single puppy
elif request.method == 'PUT':
return Response({})
@api_view(['GET', 'POST'])
def get_post_puppies(request):
# get all puppies
if request.method == 'GET':
return Response({})
# insert a new record for a puppy
elif request.method == 'POST':
return Response({})

View File

@ -123,9 +123,9 @@ USE_TZ = True
STATIC_URL = '/static/'
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permission
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [],
'TEST_REQUEST_DEFAULT_FORMAT': 'json'
}
# REST_FRAMEWORK = {
# # Use Django's standard `django.contrib.auth` permission
# # or allow read-only access for unauthenticated users.
# 'DEFAULT_PERMISSION_CLASSES': [],
# 'TEST_REQUEST_DEFAULT_FORMAT': 'json'
# }

View File

@ -15,7 +15,13 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url # for django 1.x
urlpatterns = [
url(r'^', include('puppies.urls')),
url(
r'^api-auth/',
include('rest_framework.urls', namespace='rest_framework')
),
path('admin/', admin.site.urls),
]