https://github.com/quvonchbekbobojonov/django-success-response
Django app for customizing response
https://github.com/quvonchbekbobojonov/django-success-response
django django-application django-rest-framework django-success-response
Last synced: 10 days ago
JSON representation
Django app for customizing response
- Host: GitHub
- URL: https://github.com/quvonchbekbobojonov/django-success-response
- Owner: QuvonchbekBobojonov
- License: mit
- Created: 2024-12-05T05:15:42.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-01-29T04:32:37.000Z (9 months ago)
- Last Synced: 2025-04-09T03:36:02.429Z (6 months ago)
- Topics: django, django-application, django-rest-framework, django-success-response
- Language: Python
- Homepage: https://django-success-response.moorfo.uz
- Size: 176 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# Django Success Response
`django-success-response` is a Django REST Framework extension that standardizes success and error response formats in API views. It simplifies response handling by providing a consistent structure and offers easy customization for data formats.
Official Docs: [django-success-response.moorfo.uz](http://django-success-response.moorfo.uz/)
## Installation
Install the package via pip:
```bash
pip install django-success-response
```## Usage
### Standard Success Response
To return a standard success response, use `SuccessResponse` in your Django views.
#### Example:
```python
from success_response.response import SuccessResponse
from rest_framework.views import APIViewclass MyView(APIView):
@staticmethod
def get(request):
data = {'key': 'value'}
return SuccessResponse(data)
```#### Response:
```json
{
"success": true,
"result": {
"key": "value"
}
}
```### Error Response
For error responses, set `success=False` and provide an error message.
#### Example:
```python
from success_response.response import SuccessResponse
from rest_framework.views import APIViewclass MyView(APIView):
@staticmethod
def get(request):
data = {'message': 'error'}
return SuccessResponse(data, success=False)
```#### Response:
```json
{
"success": false,
"error": {
"message": "error"
}
}
```## Error Handling
To format all error responses using the `SuccessResponse` structure, configure the `EXCEPTION_HANDLER` in your `settings.py`:
```python
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'success_response.views.success_exception_handler'
}
```## Generic Views and ViewSets
`django-success-response` provides customized DRF generic views and viewsets that automatically return responses in the `SuccessResponse` format.
### Available Views and ViewSets:
| 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`.