资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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://cdkjz.cn/article/dggepc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220