{"id":256117,"date":"2022-11-11T08:45:24","date_gmt":"2022-11-11T00:45:24","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=256117"},"modified":"2022-10-28T12:46:24","modified_gmt":"2022-10-28T04:46:24","slug":"django-ajax-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/django-ajax-linux.html","title":{"rendered":"Django+Ajax\u5f02\u6b65\u5237\u65b0\/\u5b9a\u65f6\u81ea\u52a8\u5237\u65b0\u5b9e\u4f8b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nAJAX\u662f\u524d\u7aef\u6280\u672f\u7684\u96c6\u5408,\u5305\u62ecJavaScript\u3001XML\u3001HTML\u3001CSS\u7b49,\u4e0b\u9762\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8eDjango+Ajax\u5f02\u6b65\u5237\u65b0\/\u5b9a\u65f6\u81ea\u52a8\u5237\u65b0\u7684\u76f8\u5173\u8d44\u6599,\u6587\u4e2d\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5206\u4eab\u4e00\u4e0b\u6700\u8fd1\u5728\u5b66\u4e60Django\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u548c\u89e3\u51b3\u7684\u4e00\u4e9b\u6709\u8da3\u7684\u65b9\u6cd5\u548c\u95ee\u9898<\/p>\n

\u4e00\u3001Django\u662f\u4ec0\u4e48\uff1f<\/strong><\/div>\n

Django\u4e5f\u4e0d\u7528\u5728\u8fc7\u591a\u7684\u53bb\u4ecb\u7ecd\u4e86\u3002\u4f7f\u7528python\u8fdb\u884cweb\u5f00\u53d1\u7684\u57fa\u672c\u4e0a\u90fd\u5728\u7528\u7740\u6846\u67b6\uff0c\u53cd\u6b63\u5927\u5bb6\u90fd\u5728\u7528\uff0c\u54c8\u54c8\u54c8<\/p>\n

\u4e8c\u3001Ajax\u5f02\u6b65\u5237\u65b0<\/strong><\/div>\n
1.jQuery\u8bed\u6cd5\u4e0b\u7684Ajax\u8fd0\u7528<\/strong><\/span><\/div>\n

\u4ee3\u7801\u5982\u4e0b\uff08\u793a\u4f8b\uff09\uff1a<\/p>\n

$.ajax(\r\n          {\r\n              type:\"GET\",\r\n              url:\"\/ajax_loadavg\",\r\n              dataType:\"json\",\r\n              success:function (data) {\r\n                  json_data = data\r\n                  }<\/pre>\n

\u5f53\u7136\u5927\u5bb6\u522b\u5fd8\u4e86\u5728\u9875\u9762\u5f15\u5165jQuery\u7684\u6e90<\/p>\n

< script src=\"{% static 'Privilege\/vendor\/jquery\/jquery.min.js'%}\">< \/script><\/pre>\n

\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u6570\u636e\u66f4\u65b0\uff0c\u505a\u5230\u53ea\u5237\u65b0\u90e8\u5206\u9875\u9762\u800c\u4e0d\u9700\u8981\u6574\u4e2a\u9875\u9762\u8fdb\u884c\u5237\u65b0\uff01\uff01\uff01<\/p>\n

2.\u5b9a\u65f6\u5237\u65b0\u9875\u9762\u7684\u6837\u4f8b<\/strong><\/span><\/div>\n

\u8fd9\u91cc\u9762\u4f7f\u7528chartist\u54cd\u5e94\u5f0f\u56fe\u6807\u4f5c\u4e3a\u6848\u4f8b\uff0c\u8bbe\u5b9a\u6bcf15s\u4e2d\u5bf9\u56fe\u8868\u8fdb\u884c\u4e00\u6b21\u5237\u65b0\u3002<\/p>\n

HTML5\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n

< div id=\"headline-chart\" class=\"ct-chart\">< \/div><\/pre>\n

jQuery\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n

setInterval(function () {\r\n $.ajax(\r\n           {\r\n               type:\"GET\",\r\n               url:\"\/ajax_loadavg\",\r\n               dataType:\"json\",\r\n               success:function (data) {\r\n                   json_data = data\r\n                   \r\n           data = {\r\n               labels: [0, 10, 20, 30, 40, 50, 60],\r\n               series: [\r\n                   [1, 2, 3, 4, 0, 0, 0],\r\n                   [0, 0, 0, 0, 0, 0, 0],\r\n                   [0, 0, 0, 0, 0, 0, 0],\r\n               ]\r\n           };\r\n \r\n           options = {\r\n               height: 300,\r\n               showArea: true,\r\n               showLine: false,\r\n               showPoint: false,\r\n               fullWidth: true,\r\n               axisX: {\r\n                   showGrid: false,\r\n                showLabel: false\r\n               },\r\n               lineSmooth: false,\r\n               };\r\n \r\n           new Chartist.Line('#headline-chart', data, options);\r\n        }\r\n        },15000)<\/pre>\n

Django \u89c6\u56fe\uff08view\uff09\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\u6ce8\u610f\uff1a\u6b64\u5904\u53ea\u662f\u5c55\u793aAjax\u4e0eDjango\u5982\u4f55\u8fdb\u884c\u4ea4\u4e92\uff0cAjax\u83b7\u53d6\u7684JSON\u6570\u636e\u5e76\u672a\u8fdb\u884c\u5229\u7528\uff01<\/p>\n

@csrf_exempt\r\ndef ajax_loadavg(request):\r\n    if request.method == 'GET':\r\n        context = {'output_loadavg':output_loadavg}\r\n        return HttpResponse(json.dumps(context))<\/pre>\n

url\u914d\u7f6e<\/p>\n

path('ajax_loadavg\/', views.ajax_loadavg, name='ajax_loadavg'),<\/pre>\n
3.\u5c55\u793a\u6548\u679c<\/strong><\/span><\/div>\n

\u53ef\u4ee5\u66ff\u6362series\u90e8\u5206\u7684\u6570\u636e\uff0c\u8fdb\u884c\u5c55\u793a<\/p>\n

\"\"<\/p>\n

\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53c2\u770b\uff0c\u4e5f\u5e0c\u671b\u6709\u60f3\u6cd5\u7684\u5c0f\u4f19\u4f34\uff0c\u8bc4\u8bba\u533a\u591a\u591a\u53d1\u8868\u610f\u89c1\uff01\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5206\u4eab\u4e00\u4e0b\u6700\u8fd1\u5728\u5b66\u4e60Django\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u548c\u89e3\u51b3\u7684\u4e00\u4e9b\u6709\u8da3\u7684\u65b9\u6cd5\u548c\u95ee\u9898 Django\u4e5f\u4e0d\u7528\u5728\u8fc7\u591a\u7684\u53bb\u4ecb\u7ecd\u4e86\u3002\u4f7f […]<\/p>\n","protected":false},"author":362,"featured_media":112352,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-256117","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=256117"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256117\/revisions"}],"predecessor-version":[{"id":256121,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256117\/revisions\/256121"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/112352"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=256117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=256117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=256117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}