example-of-crud-in-django-w.../app/libros/views.py

22 lines
667 B
Python
Raw Normal View History

2021-07-12 07:01:26 +02:00
# app/libros/views.py
2021-07-05 15:56:53 +02:00
from django.http import JsonResponse
2021-07-12 07:01:26 +02:00
from rest_framework.views import APIView # nuevo
from rest_framework.response import Response # nuevo
from rest_framework import status # nuevo
from .serializers import LibroSerializer # nuevo
2021-07-05 15:56:53 +02:00
def ping(request):
data = {"ping": "pong!"}
return JsonResponse(data)
2021-07-12 07:01:26 +02:00
class LibrosList(APIView):
def post(self, request):
serializer = LibroSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)