12👍
Django cannot convert Exception object to JSON format and raise error. To fix it you should convert error to string and pass result to response:
except Exception as e:
return Response(str(e), status=status.HTTP_404_NOT_FOUND, template_name=None, content_type=None)
0👍
first
import json
from django.http import HttpResponse
Change line
return Response(result, status=status.HTTP_200_OK, template_name=None, content_type=None)
for this
return HttpResponse(json.dumps(result),content_type="application/json")
or use
from django.http import JsonResponse
return JsonResponse(json.dumps(result))
- How to update a file location in a FileField?
- Python import as tuple
- Django multiple foreign key, Same related name
- Django admin – group permissions to edit or view models
0👍
Python exceptions are not json serializable.
It’s failing in try
because of some connection or content unavailable issue, then going into except
block where you are passing exception e
as it is to Response()
so that is creating the issue. Solution – check the URL and also in except block convert exception e
to string and pass to Response(str(e), status=status.HTTP_404_NOT_FOUND, template_name=None, content_type=None)
.
- Where do I install Twitter Bootstrap for Django – main or static folder?
- Django-allauth: Only allow users from a specific google apps domain
- How to find top-X highest values in column using Django Queryset without cutting off ties at the bottom?
- Pytest and Django settings runtime changes
- Django Admin, sort with custom function
- How to filter search by values that are not available
- Can i access the response context of a view tested without the test client?
- Problem reusing serializers with django and drf-yasg
Source:stackexchange.com