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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdfdb693c36e3e6a3f4bf0e7b42252ef19402f3e9ff13fb7445574c07edacbb0 |
|
MD5 | 565f8a92746ce29fbdcc25b6868ceeb5 |
|
BLAKE2b-256 | 973a516bda8156bebea5bab6eb8ab17fb349a3fd0cc8948e26ad64f1acac09f1 |
Close
Hashes for django_pretty_response-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48581494791e0b323f42784703cb93b36d510a4bb2209c9e3bc6504be97f7bd2 |
|
MD5 | f679f7ac4573e76d309fc0b6048af5ec |
|
BLAKE2b-256 | 066d46c5fff05d6fadc16c4d883a1b6c90025369d9f8238c05a9e42313052270 |