2016-07-14 59 views
1

我正在爲我的django web應用程序創建移動應用程序。我有它設置像這樣將JSON數據發送到客戶端django

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做出輕鬆響應我的移動應用程序客戶端在迅速

回答

2

看看JsonResponse

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