Skip to main content

Django app for customizing response

Project description

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

Installation

Install the package via pip:

pip install django-success-response

Usage

Standard Success Response

To return a standard success response, use SuccessResponse in your Django views.

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)

Response:

{
    "success": true,
    "result": {
        "key": "value"
    }
}

Error Response

For error responses, set success=False and provide an error message.

Example:

from success_response.response import SuccessResponse
from rest_framework.views import APIView

class MyView(APIView):
    @staticmethod
    def get(request):
        data = {'message': 'error'}
        return SuccessResponse(data, success=False)

Response:

{
    "success": false,
    "error": {
        "message": "error"
    }
}

Error Handling

To format all error responses using the SuccessResponse structure, configure the EXCEPTION_HANDLER in your settings.py:

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_success_response-1.1.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_success_response-1.1.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file django_success_response-1.1.1.tar.gz.

File metadata

  • Download URL: django_success_response-1.1.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for django_success_response-1.1.1.tar.gz
Algorithm Hash digest
SHA256 cc2f5a9806e0c81bc8c0e82349c58403d9fa14fbb4d1aba65c393bfb4f9b5788
MD5 fa8cea0ae85ca00cdcfb10a47ec2195a
BLAKE2b-256 f13ade78747f1d98bb1efda0dec555696b3785c47d3893cec730368efa8fd640

See more details on using hashes here.

File details

Details for the file django_success_response-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_success_response-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eeecdb63bb52203cb27cca290de8e788d6aefbd589006d4785245737cb1903ee
MD5 a958a8a7b17db4d1df7d59099643cfe0
BLAKE2b-256 02b1ff6d200520173ca1f3d46cda0cc454b66f8b970acf33f8d7b6e36fb99aea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page