Django app for customizing response
View the Project on GitHub QuvonchbekBobojonov/django-success-response
django-success-response
is an extension for Django REST Framework (DRF) that standardizes success and error responses in your API views. This package simplifies handling responses by providing a consistent structure for success and error cases, making it easier to maintain and understand the API response format.
In a typical Django REST Framework project, handling success and error responses can become repetitive and inconsistent. django-success-response
helps to address this by providing a standard format for all responses.
Using this package, you can:
success
and result
keys.success=False
flag.To get started with django-success-response
, install it via pip:
pip install django-success-response
Once installed, you can begin using it in your Django views.
After installation, you can easily integrate SuccessResponse
into your views. Here’s a simple example:
from success_response.response import SuccessResponse
from rest_framework.views import APIView
class MyView(APIView):
@staticmethod
def get(request):
data = {'key': 'value'}
return SuccessResponse(data)
This will return a standardized success response like:
{
"success": true,
"result": {
"key": "value"
}
}
If you need to return an error response, simply set success=False
and provide a detailed error message:
from success_response.response import SuccessResponse
from rest_framework.views import APIView
class MyView(APIView):
@staticmethod
def get(request):
data = {'detail': 'error'}
return SuccessResponse(data, success=False)
This will return an error response:
{
"success": false,
"result": {
"detail": "error"
}
}
For more advanced usage, check out the Usage page.
To automatically format all error responses using the SuccessResponse
structure, configure the EXCEPTION_HANDLER
in your Django settings.py
file:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'success_response.views.success_exception_handler'
}
This will ensure that all exceptions in your DRF views are formatted consistently.
django-success-response
also provides customized versions of DRF’s generic views and viewsets. These views automatically return responses in the SuccessResponse
format, saving you time and effort.
Standard View | Success Equivalent |
---|---|
CreateAPIView |
SuccessCreateAPIView |
RetrieveAPIView |
SuccessRetrieveAPIView |
UpdateAPIView |
SuccessUpdateAPIView |
DestroyAPIView |
SuccessDestroyAPIView |
ListAPIView |
SuccessListAPIView |
RetrieveUpdateAPIView |
SuccessRetrieveUpdateAPIView |
RetrieveDestroyAPIView |
SuccessRetrieveDestroyAPIView |
RetrieveUpdateDestroyAPIView |
SuccessRetrieveUpdateDestroyAPIView |
ModelViewSet |
SuccessModelViewSet |
ReadOnlyModelViewSet |
SuccessReadOnlyModelViewSet |
These views behave like their DRF counterparts but automatically format responses using SuccessResponse
.
SuccessResponse
.If you need help or have any questions, feel free to contact us at contact@moorfo.uz. ```
This structured approach ensures users get a clear understanding of the package and how to use it effectively in their Django projects.