Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_pretty_response-1.0.2.tar.gz (2.9 kB view hashes)

Uploaded Source

Built Distribution

django_pretty_response-1.0.2-py3-none-any.whl (3.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page