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
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.