# 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
```json
{
"ok": true,
"result": { "your_defined_data": "value" }
}
```
### 😢 Failure
```json
{
"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.
```python
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
```python
from django.http import HttpResponse
def ping(request, *args, **kwargs):
return HttpResponse("pong")
```
returns
```json
{
"ok": true,
"result": "pong"
}
```
---
```python
from django.http import HttpResponse
def ping(request, *args, **kwargs):
return HttpResponse("not found", status=404)
```
returns
```json
{
"ok": false
"error": {
"status_code": 404,
"message": "not found"
}
}
```
---
```python
from django.http import HttpResponseBadRequest
def ping(request, *args, **kwargs):
return HttpResponseBadRequest("invalid parameter")
```
returns
```json
{
"ok": false
"error": {
"status_code": 400,
"message": "invalid parameter"
}
}
```
### DRF
```python
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
```json
{
"ok": true,
"result": "pong"
}
```
---
```python
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
```json
{
"ok": false
"error": {
"status_code": 400,
"code": "invalid value",
"message": "check your parameter"
}
}
```
---
You can also customize your error fields.
```python
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
```json
{
"ok": false
"error": {
"status_code": 400,
"hint": "check your parameters"
}
}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/knnarro/django-pretty-response",
"name": "django-pretty-response",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "django,middleware",
"author": "Naro",
"author_email": "knnarro@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/d7/e7/2aa41efd9c7074b60b08355ff1b4ddfc4f23babddb4b9ffdcb54e3fbc151/django_pretty_response-1.0.2.tar.gz",
"platform": null,
"description": "# django-pretty-response\nThis is a simple middleware in Django that creates clean-formatted responses, covering cases where Django and DRF are used to respond with API calls.\n### \ud83d\ude04 Success\n```json\n{\n \"ok\": true,\n \"result\": { \"your_defined_data\": \"value\" }\n}\n```\n### \ud83d\ude22 Failure\n```json\n{\n \"ok\": false\n \"error\": {\n \"status_code\": \"HTTP Status Code\",\n \"code\": \"Defined Error Code (Only supported in DRF)\",\n \"message\": \"Detailed information about the error\"\n }\n}\n```\n## Usage\nAdd `django_pretty_response.middleware.ResponseMiddleware` to the end your MIDDLEWARE in settings.py.\n```python\nMIDDLEWARE = [\n \"django.middleware.security.SecurityMiddleware\",\n \"django.contrib.sessions.middleware.SessionMiddleware\",\n \"django.middleware.common.CommonMiddleware\",\n ...\n \"django_pretty_response.middleware.ResponseMiddleware\" # Add this line!\n]\n```\n## Examples\n### Django Only\n```python\nfrom django.http import HttpResponse\n\ndef ping(request, *args, **kwargs):\n return HttpResponse(\"pong\")\n```\nreturns\n```json\n{\n \"ok\": true,\n \"result\": \"pong\"\n}\n```\n---\n```python\nfrom django.http import HttpResponse\n\ndef ping(request, *args, **kwargs):\n return HttpResponse(\"not found\", status=404)\n```\nreturns\n```json\n{\n \"ok\": false\n \"error\": {\n \"status_code\": 404,\n \"message\": \"not found\"\n }\n}\n```\n---\n```python\nfrom django.http import HttpResponseBadRequest\n\ndef ping(request, *args, **kwargs):\n return HttpResponseBadRequest(\"invalid parameter\")\n```\nreturns \n```json\n{\n \"ok\": false\n \"error\": {\n \"status_code\": 400,\n \"message\": \"invalid parameter\"\n }\n}\n```\n\n### DRF\n```python\nfrom rest_framework.response import Response\nfrom rest_framework.decorators import api_view\n\n@api_view(['GET'])\ndef ping(request, *args, **kwargs):\n return Response(\"pong\")\n```\nreturns\n```json\n{\n \"ok\": true,\n \"result\": \"pong\"\n}\n```\n---\n```python\nfrom rest_framework.response import Response\nfrom rest_framework.decorators import api_view\nfrom rest_framework.exceptions import ValidationError\n\n@api_view(['GET'])\ndef ping(request, *args, **kwargs):\n raise ValidationError(detail=\"check your parameters\", code=\"invalid value\")\n```\nreturns\n```json\n{\n \"ok\": false\n \"error\": {\n \"status_code\": 400,\n \"code\": \"invalid value\",\n \"message\": \"check your parameter\"\n }\n}\n```\n---\nYou can also customize your error fields.\n```python\nfrom rest_framework.response import Response\nfrom rest_framework.decorators import api_view\n\n@api_view(['GET'])\ndef ping(request, *args, **kwargs):\n return Response({\"hint\": \"check your parameters\"}, status=400)\n```\nreturns\n```json\n{\n \"ok\": false\n \"error\": {\n \"status_code\": 400,\n \"hint\": \"check your parameters\"\n }\n}\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A middleware in Django that creates clean-formatted responses.",
"version": "1.0.2",
"project_urls": {
"Homepage": "https://github.com/knnarro/django-pretty-response",
"Repository": "https://github.com/knnarro/django-pretty-response"
},
"split_keywords": [
"django",
"middleware"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "57fb2b9436f7caea85a5dfea4773c41ae953a1481edb0d05d00e20cca2852dee",
"md5": "cd9ea717e374dfcd8f9df204ca169119",
"sha256": "66b9f48038974cb5dee89a8a9b34a25557aa24919299f575e398cdaa456241ef"
},
"downloads": -1,
"filename": "django_pretty_response-1.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cd9ea717e374dfcd8f9df204ca169119",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 3496,
"upload_time": "2024-02-15T16:10:33",
"upload_time_iso_8601": "2024-02-15T16:10:33.674880Z",
"url": "https://files.pythonhosted.org/packages/57/fb/2b9436f7caea85a5dfea4773c41ae953a1481edb0d05d00e20cca2852dee/django_pretty_response-1.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d7e72aa41efd9c7074b60b08355ff1b4ddfc4f23babddb4b9ffdcb54e3fbc151",
"md5": "1cceb6963e0ebdc0951d4e0de0a05e6b",
"sha256": "fd35c7b9da28a8c75cf1ee7dd1da02f09d925f18639150ee37b14e43cc38dbfe"
},
"downloads": -1,
"filename": "django_pretty_response-1.0.2.tar.gz",
"has_sig": false,
"md5_digest": "1cceb6963e0ebdc0951d4e0de0a05e6b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 2897,
"upload_time": "2024-02-15T16:10:35",
"upload_time_iso_8601": "2024-02-15T16:10:35.533978Z",
"url": "https://files.pythonhosted.org/packages/d7/e7/2aa41efd9c7074b60b08355ff1b4ddfc4f23babddb4b9ffdcb54e3fbc151/django_pretty_response-1.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-15 16:10:35",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "knnarro",
"github_project": "django-pretty-response",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "django-pretty-response"
}