From 4e57d509defa63b7bb173990d62a5a440d02acd1 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Tue, 22 Jan 2019 14:57:15 +0800 Subject: [PATCH] 1 --- chapter2/mysite/myapp/apis.py | 7 ++++ chapter2/mysite/myapp/static/scripts/index.js | 40 ++++++++++--------- .../mysite/myapp/templates/myapp/crawl.html | 2 +- chapter2/mysite/myapp/urls.py | 1 + 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/chapter2/mysite/myapp/apis.py b/chapter2/mysite/myapp/apis.py index 0ebc68d..a256d9d 100644 --- a/chapter2/mysite/myapp/apis.py +++ b/chapter2/mysite/myapp/apis.py @@ -18,3 +18,10 @@ def crawl_info(request): def platform_info(request): _info = get_platform_info() return _render_json(_info) + +def news(request): + news = News.objects.order_by('-id').all()[0:20] + data = serialize('json', news) + response = HttpResponse(json.dumps(data, ensure_ascii=False), + content_type="application/json; charset=utf-8") + return response diff --git a/chapter2/mysite/myapp/static/scripts/index.js b/chapter2/mysite/myapp/static/scripts/index.js index 60abf9a..47f7f9d 100755 --- a/chapter2/mysite/myapp/static/scripts/index.js +++ b/chapter2/mysite/myapp/static/scripts/index.js @@ -8,28 +8,32 @@ $(function(){ //更新抓取信息 setInterval(function(){ + $.ajax('/myapp/api/news.json').done(function(datas){ + for(var i in datas){ + var data = datas[i]; + console.log(i); + } + }); - $.ajax('/myapp/api/crawl_info.json').done(function(data){ - console.log(data); - $('#crawl_info_total_count').html(data.total_count); - $('#crawl_info_today_count').html(data.today_total_count); - $('#crawl_info_product_count').html(data.product_count); - $('#crawl_info_today_product_count').html(data.today_product_count); - $('#crawl_info_comment_count').html(data.comment_count); - $('#crawl_info_today_comment_count').html(data.today_comment_count); - }); - - $.ajax('/myapp/api/platform_info.json').done(function(data){ - $('#disk_freed').html(data.disk_freed+'G'); - $('#disk_used').html(data.disk_used+'G'); - $('#mem_free').html(to_g(data.mem_total-data.mem_used)+'G'); - $('#mem_used').html(to_g(data.mem_used)+'G'); - $('#tasks_count').html(data.tasks); - }); + $.ajax('/myapp/api/crawl_info.json').done(function(data){ + console.log(data); + $('#crawl_info_total_count').html(data.total_count); + $('#crawl_info_today_count').html(data.today_total_count); + $('#crawl_info_product_count').html(data.product_count); + $('#crawl_info_today_product_count').html(data.today_product_count); + $('#crawl_info_comment_count').html(data.comment_count); + $('#crawl_info_today_comment_count').html(data.today_comment_count); + }); + $.ajax('/myapp/api/platform_info.json').done(function(data){ + $('#disk_freed').html(data.disk_freed+'G'); + $('#disk_used').html(data.disk_used+'G'); + $('#mem_free').html(to_g(data.mem_total-data.mem_used)+'G'); + $('#mem_used').html(to_g(data.mem_used)+'G'); + $('#tasks_count').html(data.tasks); + }); }, 1500); - init(); init2(); $("#el-dialog").addClass("hide"); diff --git a/chapter2/mysite/myapp/templates/myapp/crawl.html b/chapter2/mysite/myapp/templates/myapp/crawl.html index bcb19c5..f6305dc 100644 --- a/chapter2/mysite/myapp/templates/myapp/crawl.html +++ b/chapter2/mysite/myapp/templates/myapp/crawl.html @@ -149,7 +149,7 @@
{% for new in news %} -