1. views.py
定義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://newbst.com/article32/dggepc.html
成都網站建設公司_創新互聯,為您提供響應式網站、網站維護、建站公司、域名注冊、服務器托管、動態網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯