A middleware in Django that creates clean-formatted responses.
Project description
django-pretty-response
This is a simple middleware in Django that creates clean-formatted responses, covering cases where Django and DRF are used to respond with API calls.
😄 Success
{
"ok": true,
"result": { "your_defined_data": "value" }
}
😢 Failure
{
"ok": false
"error": {
"status_code": "HTTP Status Code",
"code": "Defined Error Code (Only supported in DRF)",
"message": "Detailed information about the error"
}
}
Usage
Add django_pretty_response.middleware.ResponseMiddleware
to the end your MIDDLEWARE in settings.py.
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
...
"django_pretty_response.middleware.ResponseMiddleware" # Add this line!
]
Examples
Django Only
from django.http import HttpResponse
def ping(request, *args, **kwargs):
return HttpResponse("pong")
returns
{
"ok": true,
"result": "pong"
}
from django.http import HttpResponse
def ping(request, *args, **kwargs):
return HttpResponse("not found", status=404)
returns
{
"ok": false
"error": {
"status_code": 404,
"message": "not found"
}
}
from django.http import HttpResponseBadRequest
def ping(request, *args, **kwargs):
return HttpResponseBadRequest("invalid parameter")
returns
{
"ok": false
"error": {
"status_code": 400,
"message": "invalid parameter"
}
}
DRF
from rest_framework.response import Response
from rest_framework.decorators import api_view
@api_view(['GET'])
def ping(request, *args, **kwargs):
return Response("pong")
returns
{
"ok": true,
"result": "pong"
}
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.exceptions import ValidationError
@api_view(['GET'])
def ping(request, *args, **kwargs):
raise ValidationError(detail="check your parameters", code="invalid value")
returns
{
"ok": false
"error": {
"status_code": 400,
"code": "invalid value",
"message": "check your parameter"
}
}
You can also customize your error fields.
from rest_framework.response import Response
from rest_framework.decorators import api_view
@api_view(['GET'])
def ping(request, *args, **kwargs):
return Response({"hint": "check your parameters"}, status=400)
returns
{
"ok": false
"error": {
"status_code": 400,
"hint": "check your parameters"
}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django_pretty_response-1.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd35c7b9da28a8c75cf1ee7dd1da02f09d925f18639150ee37b14e43cc38dbfe |
|
MD5 | 1cceb6963e0ebdc0951d4e0de0a05e6b |
|
BLAKE2b-256 | d7e72aa41efd9c7074b60b08355ff1b4ddfc4f23babddb4b9ffdcb54e3fbc151 |
Close
Hashes for django_pretty_response-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66b9f48038974cb5dee89a8a9b34a25557aa24919299f575e398cdaa456241ef |
|
MD5 | cd9ea717e374dfcd8f9df204ca169119 |
|
BLAKE2b-256 | 57fb2b9436f7caea85a5dfea4773c41ae953a1481edb0d05d00e20cca2852dee |