Пример #1
0
def journey_detail(request, pk):
    """
    Retrieve, update or delete a journey.
    """
    try:
        journey = Journey.objects.get(pk=pk)
    except Journey.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = JourneySerializer(journey)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = JourneySerializer(journey, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        else:
            return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        journey.delete()
        return HttpResponse(status=204)
Пример #2
0
def journey_detail(request, pk):
    """
    Retrieve, update or delete a journey.
    """
    try:
        journey = Journey.objects.get(pk=pk)
    except Journey.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = JourneySerializer(journey)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = JourneySerializer(journey, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        else:
            return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        journey.delete()
        return HttpResponse(status=204)
Пример #3
0
def journey_list(request):
    """
    List all journeys, or create a new journey.
    """
    if request.method == 'GET':
        journeys = Journey.objects.all()
        serializer = JourneySerializer(journeys, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = JourneySerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        else:
            return JSONResponse(serializer.errors, status=400)
Пример #4
0
def journey_list(request):
    """
    List all journeys, or create a new journey.
    """
    if request.method == 'GET':
        journeys = Journey.objects.all()
        serializer = JourneySerializer(journeys, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = JourneySerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        else:
            return JSONResponse(serializer.errors, status=400)