Skip to content

Instantly share code, notes, and snippets.

@develhopper
Created August 30, 2023 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save develhopper/73045712b394c2c2439d5e97ac4dbf1b to your computer and use it in GitHub Desktop.
Save develhopper/73045712b394c2c2439d5e97ac4dbf1b to your computer and use it in GitHub Desktop.
Django response helper
from rest_framework.response import Response
def success(message,data=None,status=200):
result = {
"message":message,
"result":data
}
if page:
result['page'] = page
return Response(result,status)
def fail(message,status=400,data=None):
result = {
"message":message,
"result":data
}
return Response(result,status)
from utils.response import success,fail
def get_post(requests):
id = request.data.get('id',None)
post = Posts.object.filter(id=id).first()
if not post:
return fail("Post not found",404)
return success("Message",posts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment