python - 将 JSON 数据发送到客户端 django

标签 python json django swift

我正在为我的 Django 网络应用程序创建移动应用程序。我是这样设置的

模型.py

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

View .py

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



      jsonString = #create an organized json string from comment models

      return HttpResponse(jsonString)

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

最佳答案

查看JsonResponse :

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

关于python - 将 JSON 数据发送到客户端 django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382514/

相关文章:

javascript - 如何在 JSON 中获取随机值

javascript - 使用 Django Ajax 成功后创建新的 Google map

django - 如何使用 django-pytest 跟踪 Django 重定向?

python - 如何在 Django 的单元测试期间禁用翻译?

python - 用 python 估计欧几里德变换

javascript - 使用来自 json 响应的动态布局 : angularJS

python - Python App Engine 上的 GWT

Django 日期时间任务友好格式

python - 派克: 'dynamically' append to qtextedit from function

python - 在 Django 中处理用户触发事件的正确方法是什么?