コード例 #1
0
def app_list(request):
    """
    Api to deal with all app instances, get, put (add).
    """
    if request.method == 'GET':
        apps = App.objects.all()
        serializer = AppSerializer(apps, many=True)
        return HttpResponse(JSONRenderer().render(serializer.data), {'content_type' : 'application/json'})

    elif request.method == 'POST':
        serializer = AppSerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return HttpResponse(JSONRenderer().render(serializer.data), {'content_type' : 'application/json'})
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #2
0
def app_detail(request, app_id):
    """
    Api to deal with single app instances, get, put (add) or delete.
    """
    app = get_object_or_404(App, pk=app_id)

    if request.method == 'GET':
        serializer = AppSerializer(app)
        return HttpResponse(JSONRenderer().render(serializer.data), {'content_type' : 'application/json'})

    elif request.method == 'PUT':
        serializer = AppSerializer(app, data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return HttpResponse(JSONRenderer().render(serializer.data), {'content_type' : 'application/json'})
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        app.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)