Reqest Response
This commit is contained in:
parent
d414868d93
commit
01035a2b45
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
venv
|
venv
|
||||||
.vscode
|
.vscode
|
||||||
__pycache__
|
__pycache__
|
||||||
|
db.sqlite3
|
Binary file not shown.
@ -1,7 +1,10 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
from rest_framework.urlpatterns import format_suffix_patterns
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('snippets/', views.snippet_list),
|
path('snippets/', views.snippet_list),
|
||||||
path('snippets/<int:pk>/', views.snippet_detail),
|
path('snippets/<int:pk>/', views.snippet_detail),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
urlpatterns = format_suffix_patterns(urlpatterns)
|
@ -1,49 +1,47 @@
|
|||||||
from django.http import HttpResponse, JsonResponse
|
from rest_framework import status
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from rest_framework.decorators import api_view
|
||||||
from rest_framework.parsers import JSONParser
|
from rest_framework.response import Response
|
||||||
from .models import Snippet
|
from .models import Snippet
|
||||||
from .serializers import SnippetSerializer
|
from .serializers import SnippetSerializer
|
||||||
|
|
||||||
@csrf_exempt
|
@api_view(['GET', 'POST'])
|
||||||
def snippet_list(request):
|
def snippet_list(request, format=None):
|
||||||
"""
|
"""
|
||||||
List all code snippets, or create a new snippet.
|
List all code snippets, or create a new snippet.
|
||||||
"""
|
"""
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
snippets = Snippet.objects.all()
|
snippets = Snippet.objects.all()
|
||||||
serializer = SnippetSerializer(snippets, many=True)
|
serializer = SnippetSerializer(snippets, many=True)
|
||||||
return JsonResponse(serializer.data, safe=False)
|
return Response(serializer.data)
|
||||||
|
|
||||||
elif request.method == 'POST':
|
elif request.method == 'POST':
|
||||||
data = JSONParser().parse(request)
|
serializer = SnippetSerializer(data=request.data)
|
||||||
serializer = SnippetSerializer(data=data)
|
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
return JsonResponse(serializer.data, status=201)
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
return JsonResponse(serializer.errors, status=400)
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
@csrf_exempt
|
@api_view(['GET', 'PUT', 'DELETE'])
|
||||||
def snippet_detail(request, pk):
|
def snippet_detail(request, pk, format=None):
|
||||||
"""
|
"""
|
||||||
Retrieve, update or delete a code snippet.
|
Retrieve, update or delete a code snippet.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
snippet = Snippet.objects.get(pk=pk)
|
snippet = Snippet.objects.get(pk=pk)
|
||||||
except Snippet.DoesNotExist:
|
except Snippet.DoesNotExist:
|
||||||
return HttpResponse(status=404)
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
serializer = SnippetSerializer(snippet)
|
serializer = SnippetSerializer(snippet)
|
||||||
return JsonResponse(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
elif request.method == 'PUT':
|
elif request.method == 'PUT':
|
||||||
data = JSONParser().parse(request)
|
serializer = SnippetSerializer(snippet, data=request.data)
|
||||||
serializer = SnippetSerializer(snippet, data=data)
|
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
return JsonResponse(serializer.data)
|
return Response(serializer.data)
|
||||||
return JsonResponse(serializer.errors, status=400)
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
elif request.method == 'DELETE':
|
elif request.method == 'DELETE':
|
||||||
snippet.delete()
|
snippet.delete()
|
||||||
return HttpResponse(status=204)
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
Reference in New Issue
Block a user