Welcome to django-success-response Documentation
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.
Key Features
Standardized Response Format: Ensures that all API responses follow a uniform structure, making your API easier to use and maintain.
Simplifies Response Handling: Automatically formats success and error responses in your DRF views.
Customizable Error Handling: Provides an easy way to handle errors and return structured error responses.
Integration with DRF Views: Seamlessly integrates with Django REST Framework’s generic views and viewsets to automatically format responses.
Why Use django-success-response?
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:
Standardize the response structure: All responses are formatted with
success
andresult
keys.Reduce boilerplate code: Automatically formats responses without the need to manually structure each response.
Easily handle errors: Return structured error messages using the
success=False
flag.
Table of Contents
Usage
Generic Views and ViewSets
API Reference
Changelog
Installation
To get started with django-success-response
, install it via pip:
Once installed, you can begin using it in your Django views.
Basic Example
After installation, you can easily integrate SuccessResponse
into your views. Here’s a simple example:
This will return a standardized success response like:
Error Response
If you need to return an error response, simply set success=False
and provide a detailed error message:
This will return an error response:
For more advanced usage, check out the Usage page.
Error Handling
To automatically format all error responses using the SuccessResponse
structure, configure the EXCEPTION_HANDLER
in your Django settings.py
file:
This will ensure that all exceptions in your DRF views are formatted consistently.
Generic Views and ViewSets
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.
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
.
Documentation
Installation: Learn how to install the package.
Usage: Explore examples and advanced usage of
SuccessResponse
.Error Handling: Learn how to configure global error handling.
Generic Views and ViewSets: Explore customized DRF views and viewsets for automatic response handling.
API Reference: Detailed reference for all available methods and settings.
Changelog: View changes and updates in the package.
If you need help or have any questions, feel free to contact us at contact@moorfo.uz.
Explanation of Sections:
Introduction: A brief description of the package and its purpose.
Key Features: Lists the core features of the package, focusing on standardization and simplicity.
Why Use django-success-response?: Explains the problem the package solves and the benefits it brings.
Table of Contents: Links to different sections of the documentation for easy navigation.
Installation: Provides simple steps to install the package via pip.
Basic Example: A code example demonstrating how to use the package to return a success response.
Error Response: Shows how to handle error responses using the package.
Error Handling: Explains how to globally handle exceptions in DRF using the package’s handler.
Generic Views and ViewSets: Highlights the convenience of using the customized DRF views and viewsets that automatically use the success response format.
Documentation: Lists links to other important sections in the documentation.
Contact Info: Provides a way for users to reach out for help or queries.
This structured approach ensures users get a clear understanding of the package and how to use it effectively in their Django projects.
Last updated