最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

django ajax json的實(shí)例代碼

 更新時(shí)間:2018年05月29日 08:43:12   作者:q493383189  
今天就為大家分享一篇django ajax json的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

1. views.py

定義views視圖函數(shù),將數(shù)據(jù)存入字典。并用壓縮為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)

2. article.html中編輯js jquery,接受json數(shù)據(jù),并處理并添加到html中

<script>
 function getComments() {
 $.get("{% url 'get_comment' one_article.id %}", function(callback){
 console.log(callback);
 var obj = JSON.parse(callback);
 console.log(this.comment_type);
 for (var key in obj){
 console.log(key);
 console.log(obj[key])
 }
 }
 function getCsrf() {
 return $("input[name='csrfmiddlewaretoken']").val();
 }
 $(document).ready(function () {
 $(".comment-box button").click(function () {
 var comment_text = $('.comment-box textarea').val();
 if (comment_text.trim().length < 5){
 alert("評(píng)論不能少于5個(gè)字")
 }else {
 $.post(
  "{% url 'post_comment' %}",
  {
  'comment_type':1,
  article_id: "{{ one_article.id }}",
  parent_comment_id:null,
  'comment':comment_text.trim(),
  'csrfmiddlewaretoken':getCsrf()
  },
  function (callback) {
  console.log(callback);
  if (callback == 'post-comment-success'){
  alert('post-comment-success');
  getComments();
  }
  }
 )
 }
 })
 })
</script>

以上這篇django ajax json的實(shí)例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python requests模塊的使用示例

    python requests模塊的使用示例

    這篇文章主要介紹了python requests模塊的使用解析,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • Python編程實(shí)戰(zhàn)之Oracle數(shù)據(jù)庫操作示例

    Python編程實(shí)戰(zhàn)之Oracle數(shù)據(jù)庫操作示例

    這篇文章主要介紹了Python編程實(shí)戰(zhàn)之Oracle數(shù)據(jù)庫操作,結(jié)合具體實(shí)例形式分析了Python的Oracle數(shù)據(jù)庫模塊cx_Oracle包安裝、Oracle連接及操作技巧,需要的朋友可以參考下
    2017-06-06
  • python編程實(shí)現(xiàn)希爾排序

    python編程實(shí)現(xiàn)希爾排序

    這篇文章主要介紹了python實(shí)現(xiàn)希爾排序,已編程實(shí)現(xiàn)的希爾排序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • python生成指定長(zhǎng)度的隨機(jī)數(shù)密碼

    python生成指定長(zhǎng)度的隨機(jī)數(shù)密碼

    這篇文章主要介紹了python生成指定長(zhǎng)度的隨機(jī)密碼示例,密碼使用數(shù)字和字母組合,大家參考使用吧
    2014-01-01
  • python實(shí)現(xiàn)手機(jī)號(hào)歸屬地查詢功能

    python實(shí)現(xiàn)手機(jī)號(hào)歸屬地查詢功能

    手機(jī)上突然收到了某銀行的短信提示,看了一下手機(jī)的位數(shù),正好是11位,我一想,這不就是標(biāo)準(zhǔn)的手機(jī)號(hào)碼嗎?于是想用python的庫實(shí)現(xiàn)查詢手機(jī)號(hào)碼歸屬地查詢自由,所以本文給大家介紹了如何用python實(shí)現(xiàn)手機(jī)號(hào)歸屬地查詢功能,需要的朋友可以參考下
    2024-03-03
  • python序列類型種類詳解

    python序列類型種類詳解

    這篇文章主要介紹了python序列類型種類詳解,需要的朋友們可以學(xué)習(xí)參考下。
    2020-02-02
  • Python 異常處理總結(jié)

    Python 異常處理總結(jié)

    閱讀本篇文章以學(xué)習(xí) Python 異常處理。它應(yīng)該可以幫助您了解如何在程序中使用 try、except 和 finally 語句。下面跟著小編一起來學(xué)習(xí)文章內(nèi)容吧
    2021-09-09
  • 淺談Python中的全局鎖(GIL)問題

    淺談Python中的全局鎖(GIL)問題

    今天小編就為大家分享一篇淺談Python中的全局鎖(GIL)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python 性能優(yōu)化方法小結(jié)

    python 性能優(yōu)化方法小結(jié)

    本文主要介紹了python 提高性能的方法。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-03-03
  • Python Numpy數(shù)組擴(kuò)展repeat和tile使用實(shí)例解析

    Python Numpy數(shù)組擴(kuò)展repeat和tile使用實(shí)例解析

    這篇文章主要介紹了Python Numpy數(shù)組擴(kuò)展repeat和tile使用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12

最新評(píng)論

宿迁市| 沙雅县| 徐水县| 清新县| 上思县| 巴林左旗| 木里| 佛学| 德令哈市| 美姑县| 江门市| 稻城县| 西安市| 山东| 沙湾县| 沙坪坝区| 沭阳县| 客服| 莎车县| 浪卡子县| 墨江| 托克托县| 资兴市| 徐闻县| 隆子县| 开江县| 海口市| 肥乡县| 六盘水市| 祁连县| 奎屯市| 河津市| 河东区| 苗栗市| 西乌珠穆沁旗| 中方县| 濮阳县| 烟台市| 中西区| 洛浦县| 永善县|