Exemple #1
0
def show_list_json(request):
    """
    List all TV shows or create a new TV show.
    """
    if request.method == 'GET':
        show_list = Show.objects.all()
        serializer = ShowSerializer(show_list, many=True)
        return JSONResponse(serializer.data)
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = ShowSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)
Exemple #2
0
def show_detail_json(request, pk):
    """
    Retrieve, update or delete a TV show.
    """
    try:
        show = Show.objects.get(pk=pk)
    except Show.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = ShowSerializer(show)
        return JSONResponse(serializer.data)
    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = ShowSerializer(show, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)
    elif request.method == 'DELETE':
        show.delete()
        return HttpResponse(status=200)