成都网站建设设计

将想法与焦点和您一起共享

djangoajaxjson的实例代码-创新互联

1. views.py

创新互联公司,为您提供网站建设成都网站制作、网站营销推广、网站开发设计,对服务搅拌罐车等多个行业拥有丰富的网站建设及推广经验。创新互联公司网站建设公司成立于2013年,提供专业网站制作报价服务,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏心悦目的作品。 与客户共同发展进步,是我们永远的责任!

定义views视图函数,将数据存入字典。并用压缩为json格式,dumps,并return。

import json
def get_comments(request, article_id):
 article_obj = models.Article.objects.get(id=article_id)
 article_comments = article_obj.comment_set.select_related()
 comment_dict = {}
 for i in article_comments:
 print('comments_id', i.id)
 print('article_id', i.article_id)
 print('parent_comment_id', i.parent_comment_id)
 print('comment_type', i.comment_type)
 print('user_id', i.user_id)
 print('user_name', i.user.name)
 print('comment', i.comment)
 print('date', type(i.date))
 print('date', time.strftime("%Y-%m-%d %H:%M:%S", i.date.timetuple()))
 comment_dict[i.id] = [i.comment_type, i.comment, time.strftime("%Y-%m-%d %H:%M:%S", i.date.timetuple()), i.article_id, i.user_id, i.user.name, i.parent_comment_id]
 comment_json = json.dumps(comment_dict)
 return HttpResponse(comment_json)

网站标题:djangoajaxjson的实例代码-创新互联
当前地址:http://chengdu.cdxwcx.cn/article/dggepc.html