如何将python列表发布到django - python

我设置了一个网站,可以从JSON列表中获取一些JSON列表,但是我使它在控制台中运行,该代码将过滤掉传感器的“关闭”状态,但是我不知道如何在Django上发布历史记录传感器的停机时间列表。

我试图用下面的代码发布数据,但是我什至无法加载该站点,它仍然加载127.0.0.1

from django.shortcuts import render
from django.http import HttpResponse
import json
import requests
import time

def index(request):
    url = "https://site.site.com/api/table.json?content=status=down&username=user&passhash=hash&count=200000"
    starttime=time.time()
    while True:
        response = requests.get(url)
        data = response.json()
        d = data
        result_list = [d for d in data['status%3Ddown'] if d['status'] == 'Down']
        time.sleep(10.0 - ((time.time() - starttime) % 10.0))

    return print(result_list)
   #return HttpResponse(result_list) this dont work either

我希望得到一个列表,如果可以的话,有人可以指出我如何在数据库中创建此结果的记录,我想每30秒向我的PRTG发出一次请求,以便对此进行历史记录并添加计时器任何传感器贴上标签DOWN已有多长时间

python大神给出的解决方案

嘿,我很久以前做了这个,但我会分享我的两个解决方案

最简单的方法是制作一个熊猫数据框并将其渲染为html,看起来像这样

def example_page(request):
    context = {"title" : "example"}
    url = "https://prtg.c3ntro.com/api/table.json?content=status=down&username=someuser&passhash=123&count=123"
    response = requests.get(url)
    data = response.json()
    d = data
    result_list = [d for d in data['status%3Ddown'] if d['status'] == 'Down']
    df = pd.DataFrame(result_list)
    return HttpResponse(df.to_html())

另一个是创建模型,然后使用django_tables2将其渲染到我的模板上,这是我能做的最好的事情,但是先前的答案足以分享,因为创建模型无法回答当前问题。谢谢大家的时间

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

Python:同时在for循环中添加到列表列表 - python

我想用for循环外的0索引值创建一个新列表,然后使用for循环添加到相同的列表。我的玩具示例是:import random data = ['t1', 't2', 't3'] masterlist = [['col1', 'animal1', 'an…

如何根据子列表的长度对列表列表进行排序[重复] - python

This question already has answers here: Sorting Python list based on the length of the string (7个答案) 5年前关闭。 我有以下清单a = [['a', 'b', 'c'], ['d'…