0

I am relatively new to python and django. Trying to implement a python class but get this strange syntax error

from invoice.models import Invoice
from invoice.serializers import InvoiceSerializer
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status


class InvoiceList(APIView):

    def get(self, request, format=None):
        try:
            invoices = Invoice.objects.all()
            serializer = InvoiceSerializer(invoices, many=True)
            return Response(serializer.data)
        except Invoice.DoesNotExist:
            raise Http404
        serializer = InvoiceSerializer(invoices, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = InvoiceSerializer(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


class InvoiceDetail(APIView):

    def get_object(self, pk):
        try:
            return Invoice.objects.get(pk=pk)
        except Invoice.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        invoice=self.get_object(pk)
        serializer=InvoiceSerializer(invoice)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        invoice=self.get_object(pk)
        serializer=InvoiceSerializer(invoice, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        invoice=self.get_object(pk)
        invoice.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

this is error I get. I don't understand why it's giving me this error because error seems right to me

class InvoiceDetail(APIView): ^ SyntaxError: invalid syntax

2
  • 7
    One ) is missing just before InvoiceDetail class. ie, return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST Commented Jul 9, 2019 at 10:57
  • Hint: syntax errors points to the place where the issue is detected, which can be a few (and sometimes quite a few) lines after the place where the real issue is. Commented Jul 9, 2019 at 12:03

1 Answer 1

1

In class InvoiceList change

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST

to

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.