'How to fix Server error in Django application

When I run the code in development the try block of the code runs appropriately. However, in production, only the except block gets triggered. The error that is thrown back when I test the production api on postman is Error 500. Other part of the website is functioning appropriately except this view.

class ContactView(APIView):
    permission_classes = (permissions.AllowAny,)

    def post(self, request, format=None):
        data = self.request.data

        try:
            send_mail(
                data["subject"],
                "Message from: "
                + data ["name"]
                + "\nEmail: "
                + data["email"]
                + "\n\nMessage:\n"
                + data["message"],
                "[email protected]",
                ["[email protected]"],
                fail_silently=False
            )
            contact = Contact(name=data["name"], email=data["email"], subject=data["subject"], message=data["message"])
            contact.save()
            return Response({"success": "Message sent"})

        except:
            return Response({"error":"Unable to send message"})

I haven't experienced this kind of anonymous behaviour before. I'm using MySQL database both in development and production. The project is being hosted on EC2 with Nginx server. How do I fix this. If thers is any helpful resources on this please share



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source