views.py 818 B

12345678910111213141516171819202122232425262728
  1. from django.shortcuts import render
  2. from rest_framework.decorators import api_view, permission_classes
  3. from rest_framework.permissions import AllowAny
  4. from rest_framework.response import Response
  5. from index import models, serializers
  6. @api_view(http_method_names=('GET', 'POST', 'PATCH'))
  7. @permission_classes((AllowAny,))
  8. def movies(request):
  9. context = {'request': request}
  10. if request.method == 'GET':
  11. data = serializers.MovieSerializer(models.Movie.objects.all(), many=True, context=context).data
  12. else:
  13. serializer = serializers.MovieSerializer(data=request.data, context=context)
  14. valid = serializer.is_valid()
  15. if not valid:
  16. return Response(serializer.errors, status=400)
  17. serializer.save()
  18. data = serializer.data
  19. return Response(data)