|
| 1 | +# from django.shortcuts import render |
| 2 | +from django.shortcuts import render_to_response |
| 3 | +from django.http import HttpResponse |
| 4 | +# from django.views.generic import ListView |
| 5 | +from django.conf import settings |
| 6 | +from TM.models import Temperature |
| 7 | +# import logging |
| 8 | +from django.views.decorators.csrf import csrf_exempt |
| 9 | +import sqlite3 |
| 10 | +# 查看tables:apt安装sqlite3,然后sqlite3 db.sqlite3,输入.tables。 |
| 11 | +import matplotlib |
| 12 | +matplotlib.use('Agg') |
| 13 | +import matplotlib.pyplot as plt |
| 14 | +import os |
| 15 | + |
| 16 | +# logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | +# Create your views here. |
| 19 | + |
| 20 | + |
| 21 | +@csrf_exempt |
| 22 | +def index(request): |
| 23 | + if request.method == 'POST': |
| 24 | + add = Temperature(value=request.POST.get("temperature_data", "")) |
| 25 | + add.save() # 不save无法保存到数据库 |
| 26 | + return HttpResponse('login success!') |
| 27 | + else: |
| 28 | + # 从sqlite中获取数据。 |
| 29 | + conn = sqlite3.connect('db.sqlite3') |
| 30 | + cur = conn.cursor() |
| 31 | + cur.execute("SELECT * FROM TM_Temperature") |
| 32 | + data = cur.fetchall() |
| 33 | + data_0 = [int(row[0]) for row in data][-10:] |
| 34 | + data_2 = [float(row[2]) for row in data][-10:] |
| 35 | + |
| 36 | + plot_file = 'static/TM/plot.png' |
| 37 | + fig1, ax1 = plt.subplots(figsize=(8, 4), dpi=98) |
| 38 | + ax1.set_title(u'房间温度', fontproperties='KaiTi') |
| 39 | + ax1.set_xlabel(u'时间(小时)', fontproperties='KaiTi') |
| 40 | + ax1.set_ylabel(u'温度(\u2103)', fontproperties='KaiTi') |
| 41 | + ax1.plot( |
| 42 | + data_0, |
| 43 | + data_2, |
| 44 | + ) |
| 45 | + fig1.savefig(plot_file) |
| 46 | + plt.close(fig1) |
| 47 | + |
| 48 | + # temperature_list = Temperature.objects.all() |
| 49 | + return render_to_response('TM/index.html') |
| 50 | + |
| 51 | + |
| 52 | +# * Base_Mixin |
| 53 | +# class Base_Mixin(object): |
| 54 | +# """Basic mix class.""" |
| 55 | + |
| 56 | +# def get_context_data(self, *args, **kwargs): |
| 57 | +# context = super(Base_Mixin, self).get_context_data(**kwargs) |
| 58 | +# try: |
| 59 | +# # 网站标题等内容 |
| 60 | +# context['website_title'] = settings.WEBSITE_TITLE |
| 61 | +# except Exception: |
| 62 | +# logger.error(u'[BaseMixin]加载基本信息出错') |
| 63 | +# return context |
| 64 | + |
| 65 | +# * Index_View |
| 66 | +# class Index_View(Base_Mixin, ListView): |
| 67 | +# """view for index.html""" |
| 68 | +# model = Temperature |
| 69 | +# # 或者 |
| 70 | +# # queryset = Temperature.objects.all() |
| 71 | +# template_name = 'TM/index.html' |
| 72 | +# context_object_name = 'temperature_list' |
| 73 | + |
| 74 | +# # def get(self, request, *args, **kwargs): |
| 75 | +# # article_id = self.kwargs.get('id') |
| 76 | + |
| 77 | +# # # 如果ip不存在就把文章的浏览次数+1。 |
| 78 | +# # if ip not in visited_ips: |
| 79 | +# # try: |
| 80 | +# # article = self.queryset.get(id=article_id) |
| 81 | +# # except ArticleSwint.DoesNotExist: |
| 82 | +# # logger.error(u'[ArticleView]访问不存在的文章:[%s]' % article_id) |
| 83 | +# # raise Http404 |
| 84 | +# # else: |
| 85 | +# # article.view_times += 1 |
| 86 | +# # article.save() |
| 87 | +# # visited_ips.append(ip) |
| 88 | + |
| 89 | +# # # 更新缓存 |
| 90 | +# # cache.set(article_id, visited_ips, 15 * 60) |
| 91 | + |
| 92 | +# # return super(Article_View, self).get(request, *args, **kwargs) |
| 93 | + |
| 94 | +# @csrf_exempt |
| 95 | +# def post(self, request, *args, **kwargs): |
| 96 | +# add = Temperature(value=request.POST) |
| 97 | +# add.save() # 不save无法保存到数据库 |
| 98 | +# # 或者 |
| 99 | +# # Temperature.objects.create(value=request.POST) |
| 100 | +# kwargs['Temp'] = request.POST + 1 |
| 101 | + |
| 102 | +# return super(Index_View, self).post(request, *args, **kwargs) |
0 commit comments