将JSON数据发送到客户端django

时间:2016-07-14 19:18:29

标签: python json django swift

我正在为我的django web应用程序创建移动应用程序。我有这样的设置

models.py

 class Comment(models.Model):
      CommentDescription = models.CharField(max_length=120)
      Owner = models.ForeignKey(User,null=True)
      PostToCommentOn = models.ForeignKey(Post, null=True)

views.py

  def sendJsonDataBackToMobileClient(request,postId):
      comments = Comment.objects.filter(PostToCommentOn = postId)



      jsonString = #create an organized json string from comment models

      return HttpResponse(jsonString)

我不确定如何组织我的评论模型的json来快速对我的移动应用程序客户端做出轻量级的响应

1 个答案:

答案 0 :(得分:3)

查看JsonResponse

>>> from django.http import JsonResponse
>>> response = JsonResponse({'foo': 'bar'})
>>> response.content
b'{"foo": "bar"}'