def comment_list(request): if request.method == 'GET': comments = Comment.objects.all() comments = [c.to_json() for c in comments] return JsonResponse(comments, safe=False) elif request.method == 'POST': data = json.loads(request.body) post = Post(content=data['content'], date=data['date']) post.save() return JsonResponse(post.to_json())
def post_list(request): if request.method == 'GET': posts = Post.objects.all() posts = [p.to_json() for p in posts] return JsonResponse(posts, safe=False) elif request.method == 'POST': data = json.loads(request.body) post = Post( title=data['title'], content=data['content'], date=data['date'], #created_by=data['created_by'], ) post.save() return JsonResponse(post.to_json())
def post_list(request): if request.method == 'GET': posts = Post.objects.all() for post in posts: post.to_json() return JsonResponse(Post.objects.first().to_json(), safe=False) elif request.method == 'POST': data = json.loads(request.body) plist = Post( title=data['title'], author=data['author'], date_published=data['date_published'], content=data['content'], comment=data['comment'], ) plist.save() return JsonResponse(plist.to_json())