From 955b3b1a15b70effe27e919d6c6eb90dfc37ccba Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Mon, 21 Jan 2019 17:14:38 +0800 Subject: [PATCH] 1 --- chapter2/init.sql | 1 + .../mysite/myapp/templates/myapp/result.html | 1 + chapter2/mysite/myapp/views.py | 58 +++++++++++-------- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/chapter2/init.sql b/chapter2/init.sql index f9101e7..7d7ae2b 100644 --- a/chapter2/init.sql +++ b/chapter2/init.sql @@ -88,3 +88,4 @@ create table datas ( insert into datas (`key`, data) values ('from_type', '{}'); insert into datas (`key`, data) values ('last_month_sell', '{}'); insert into datas (`key`, data) values ('hour_sell', '{}'); +insert into datas (`key`, data) values ('sentiments', '{}'); diff --git a/chapter2/mysite/myapp/templates/myapp/result.html b/chapter2/mysite/myapp/templates/myapp/result.html index 40c60fc..c0fdbb0 100644 --- a/chapter2/mysite/myapp/templates/myapp/result.html +++ b/chapter2/mysite/myapp/templates/myapp/result.html @@ -11,6 +11,7 @@ var top10_sells = {{top10_sells|safe}}; var last_month_sell = {{last_month_sell|safe}}; var hour_sell = {{hour_sell|safe}}; + var sentiments = {{sentiments|safe}}; diff --git a/chapter2/mysite/myapp/views.py b/chapter2/mysite/myapp/views.py index 86fc29d..1858e2e 100644 --- a/chapter2/mysite/myapp/views.py +++ b/chapter2/mysite/myapp/views.py @@ -1,39 +1,49 @@ from django.shortcuts import render from django.http import HttpResponse from . import scrapy_client -from myapp.models import CrawlInfos,PlatformInfos, News,LastDayCounts, Datas, Top10Sells +from myapp.models import CrawlInfos, PlatformInfos, News, LastDayCounts, Datas, Top10Sells from myapp.utils import get_platform_info import json + def index(request): - return render(request, 'myapp/index.html') + return render(request, 'myapp/index.html') + def crawl(request): - # info = scrapy_client.get_scrapy_info() - crawl_info = CrawlInfos.objects.order_by('-id').first() - platform_info = get_platform_info() - news = News.objects.order_by('-id').all()[0:20] - last_day_counts = LastDayCounts.objects.order_by("last_day").all() + # info = scrapy_client.get_scrapy_info() + crawl_info = CrawlInfos.objects.order_by('-id').first() + platform_info = get_platform_info() + news = News.objects.order_by('-id').all()[0:20] + last_day_counts = LastDayCounts.objects.order_by("last_day").all() - last_day_product = [] - last_day_comment = [] - for last_day in last_day_counts: - last_day_product.append(last_day.product_c) - last_day_comment.append(last_day.comment_c) + last_day_product = [] + last_day_comment = [] + for last_day in last_day_counts: + last_day_product.append(last_day.product_c) + last_day_comment.append(last_day.comment_c) + + return render(request, 'myapp/crawl.html', {"crawl_info": crawl_info, + "platform_info_json": json.dumps(platform_info), + "platform_info": platform_info, + "news": news, + "last_day_product": json.dumps(last_day_product), + "last_day_comment": json.dumps(last_day_comment)}) - return render(request, 'myapp/crawl.html', {"crawl_info": crawl_info, - "platform_info_json":json.dumps(platform_info), - "platform_info": platform_info, - "news": news, - "last_day_product":json.dumps(last_day_product), - "last_day_comment": json.dumps(last_day_comment)}) def result(request): - from_type_info = json.loads(Datas.objects.filter(key='from_type').first().data) - top10_sells = [entry for entry in Top10Sells.objects.order_by('id').values()] + from_type_info = json.loads( + Datas.objects.filter(key='from_type').first().data) + top10_sells = [ + entry for entry in Top10Sells.objects.order_by('id').values()] - last_month_sell = json.loads(Datas.objects.filter(key='last_month_sell').first().data) + last_month_sell = json.loads(Datas.objects.filter( + key='last_month_sell').first().data) - hour_sell = json.loads(Datas.objects.filter(key='hour_sell').first().data) - return render(request, 'myapp/result.html', {"from_type_info": from_type_info, - "top10_sells": top10_sells, "last_month_sell": last_month_sell, "hour_sell": hour_sell}) \ No newline at end of file + hour_sell = json.loads(Datas.objects.filter(key='hour_sell').first().data) + sentiments = json.loads(Datas.objects.filter(key='sentiments').first().data) + return render(request, 'myapp/result.html', {"from_type_info": from_type_info, + "top10_sells": top10_sells, + "last_month_sell": last_month_sell, + "hour_sell": hour_sell, + "sentiments": sentiments})