Skip to main content

Django app for customizing response

Project description

Django Saccess Response

django-saccess-response is a Django REST Framework extension that provides a standardized success response format for API views. It simplifies handling both successful and error responses with customizable data structures.

Installation

Install the package via pip:

pip install django-saccess-response

Usage

In your Django views, use SaccessResponse to wrap the response data.

Example: Standard Success Response

from saccess_response.response import SaccessResponse
from rest_framework.views import APIView


class MyView(APIView):
    @staticmethod
    def get(request):
        data = {'key': 'value'}
        return SaccessResponse(data)

Result:

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

Example: Error Response

To handle errors, pass saccess=False:

from saccess_response.response import SaccessResponse
from rest_framework.views import APIView


class MyView(APIView):
    @staticmethod
    def get(request):
        data = {'key': 'value'}
        return SaccessResponse(data, saccess=False)

Result:

{
    "success": false,
    "result": {
        "detail": "error"
    }
}

Error Handling

You can also customize Django REST Framework's error responses globally by modifying the EXCEPTION_HANDLER setting in your settings.py:

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'saccess_response.views.saccess_exception_handler'
}

This will format all exceptions using the SaccessResponse structure.

Generic Views and ViewSets

This package also provides customized DRF generic views and viewsets for standardized response handling.

Standard View Saccess Equivalent
CreateAPIView SaccessCreateAPIView
RetrieveAPIView SaccessRetrieveAPIView
UpdateAPIView SaccessUpdateAPIView
DestroyAPIView SaccessDestroyAPIView
ListAPIView SaccessListAPIView
RetrieveUpdateAPIView SaccessRetrieveUpdateAPIView
RetrieveDestroyAPIView SaccessRetrieveDestroyAPIView
RetrieveUpdateDestroyAPIView SaccessRetrieveUpdateDestroyAPIView
ModelViewSet SaccessModelViewSet
ReadOnlyModelViewSet SaccessReadOnlyModelViewSet

These classes behave like their DRF counterparts but automatically format responses using SaccessResponse.

License

This project is licensed under the MIT License. See the LICENSE file for details.

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_saccess_response-1.0.5.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

django_saccess_response-1.0.5-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file django_saccess_response-1.0.5.tar.gz.

File metadata

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

File hashes

Hashes for django_saccess_response-1.0.5.tar.gz
Algorithm Hash digest
SHA256 4330f1a09e1d15b68aadbb0e74dadabfa2e5dc3c9a007e7fc0e79ddb9ced84e8
MD5 f6649e124f0057a4d6b2918974951731
BLAKE2b-256 87bf628f25bfa1f981a7a3d3d66dd06a09b1f30bc4e358647a6695c859cb3ff8

See more details on using hashes here.

File details

Details for the file django_saccess_response-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_saccess_response-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 bba7897ea18ca12c245092d5ca1a794b51262d901c90d7a3374b595b9973c36a
MD5 d9763f1e7f656760da9af8f13896e928
BLAKE2b-256 8bd9e91205e2062f0d95b58861f474f73d5eb084f562f502ad43bfec96fcecd6

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