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.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78f3a3a99372e4f31bbcbee62c9491b157d105475c68d4450b4f421e537afd17 |
|
MD5 | 3020ff696d6713853f5d70318a13a1f8 |
|
BLAKE2b-256 | 464c26fd1db90e705323df1ec5c63f8c965681fea9e220e6282b333f1bfd4ee7 |
Close
Hashes for django_pretty_response-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99681dd66d4eaece4d7c33b9d6a7f2b8eb8069cb0bcace64ea0a858b1d8d9108 |
|
MD5 | 8aa951aa541933908f42aff416bdf0a9 |
|
BLAKE2b-256 | 40631dca699ed59f193ee5bade6a14f782ebd063ffd45dd658d9bac167de01b7 |