在Django模板中显示报废的数据

时间:2019-04-29 16:18:58

标签: django beautifulsoup

我试图通过剪贴并将其存储并将其传递给模板来显示来自网站的数据,但无法显示任何内容。

pic1 当用户点击这些红色按钮时,该位置将废弃铵,NO2等数据。

到目前为止,我已经可以在终端上抓取并显示结果,但无法在该表上显示。

pic2

views.py

from django.shortcuts import render, Http404
from .models import Data,pol
import urllib
import time
from bs4 import BeautifulSoup
import re
import urllib.request
import os
from django.conf import settings
import csv
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def data(request):
     Am = " ",CO = " ",NO = " ",OZ = " ",SO2 = " ",AT = " "
     PM10 = " ",BE = " ",NO2 = " ", ON = " ", pxy = " "
     TO = " ",BP = " ", PM2_5 = " ",SR = " "
     if request.method == 'POST':
         if 'rowd' in request.POST:
           rowd = request.POST['rowd']         
           station_code = {"av":"QW5hbmRWaWhhcg==","mm":"TWFuZGlybWFyZw==",
           "pb":"UHVuamFiaUJhZ2g=","rkPuram":"UktQdXJhbQ==","airpo":"SUdJ",
           "civilLines":"Q2l2aWxsaW5lcw=="}
           result = None
           #while result:
           sc = {"av":1,"mm":2,
           "pb":3,"rkPuram":4,"airpo":5,
           "civilLines":6}
           while(result is None):
                     print("station",rowd)
                     url = "http://www.dpccairdata.com/dpccairdata/display/AallStationView5MinData.php?stName={}".format(station_code[rowd])

                     html = urllib.request.urlopen(url).read()
                     soup = BeautifulSoup(html)
                     regex = re.compile('tdcolor.*')
                     td1 = soup.find_all("tr", {"class" : regex})
                     #td2 = soup.find_all("tr", {"class" : "tdcolor2"})
                     param = ["Ammonia","Carbon Monoxide","Nitrogen Oxide","Ozone","Sulphur Dioxide","Ambient Temperature"," Particulate Matter < 10 µg","Benzene","Nitrogen Dioxide","Oxides of Nitrogen","p-Xylene","Toluene","Barometric Pressure"," Particulate Matter < 2.5 µg","Solar Radiation"]

                     for i in td1:
                         td_list = i.find_all("td")
                         if td_list[0].text in param:

                             val = td_list[3].text.split(" ")[0]
                             if param.index(td_list[0].text) == 0:
                                 Ammonia = val
                                 print("AMMONIA:",val)
                                 Am = val
                             elif param.index(td_list[0].text) == 1:
                                 Carbon_Monoxide = val
                                 print("CO:",val)
                                 CO = val
                             elif param.index(td_list[0].text) == 2:
                                 Nitrogen_Oxide = val
                                 print("NO:",val)
                                 NO = val
                             elif param.index(td_list[0].text) == 3:
                                 Ozone = val
                                 print("OZONE:",val)
                                 OZ = val
                             elif param.index(td_list[0].text) == 4:
                                 Sulphur_Dioxide = val
                                 print("SO2:",val)
                                 SO2 = val
                             elif param.index(td_list[0].text) == 5:
                                 Ambient_Temperature = val
                                 print("AMBIENT TEMP:",val)
                                 AT = val
                             elif param.index(td_list[0].text) == 6:
                                 Particulate_Matter_10 = val
                                 print("PPP10:",val)
                                 PM10 = val
                             elif param.index(td_list[0].text) == 7:
                                 Benzene = val
                                 print("BENZENE:",val)
                                 BE = val
                             elif param.index(td_list[0].text) == 8:
                                 Nitrogen_Dioxide = val
                                 print("NO2:",val)
                                 NO2 = val
                             elif param.index(td_list[0].text) == 9:
                                 Oxides_of_Nitrogen = val
                                 print("ON:",val)
                                 ON = val
                             elif param.index(td_list[0].text) == 10:
                                 p_Xylene = val
                                 print("P_XYLENE:",val)
                                 pxy = val
                             elif param.index(td_list[0].text) == 11:
                                 Toluene = val
                                 print("TOLUENE:",val)
                                 TO = val
                             elif param.index(td_list[0].text) == 12:
                                 Barometric_Pressure = val
                                 print("BAROMETRIC:",val)
                                 BP = val
                             elif param.index(td_list[0].text) == 13:
                                 Particulate_Matter_2 = val
                                 print("PPO2:",val)
                                 PM2_5 = val
                             elif param.index(td_list[0].text) == 14:
                                 Solar_Radiation = val
                                 print("Solar_rad:",val)
                                 SR = val

                     #time.sleep(1000)                    
                     result = 1                  
     context ="Ammonia":Am,"Carbon_Monoxide":CO,"Nitrogen_Oxide":NO,"Ozone":OZ,"Sulphur_Dioxide":SO2,"Ambient_Temperature":AT,"Particulate_Matter_10":PM10          ,"Benzene":BE,"Nitrogen_Dioxide":NO2,"Oxides_of_Nitrogen":ON,"p_Xylene":pxy,"Toluene":TO,"Barometric_Pressure":BP,"Particulate_Matter_2":PM2_5,"Solar_Radiation":SR}
     print(context)
     return render(request,"aqp/template.html",context)   

urls.py

from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

admin.autodiscover()
from aqp import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.home, name='home'),
    url(r'^about', views.about, name='about'),
    url(r'^contact', views.contact, name='contact'),
    url(r'^real_time', views.data, name='data'),
    url(r'^stats', views.stat, name='stat'),

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

template.html

<style>
.parent {
  display: inline-block; 
  width: 100%;

}
.left {
  float: left;
}
.right {
  float: left;

}


.filterable {
    margin-left:30px;
    margin-top: 0px;
}
.filterable .panel-heading .pull-right {
    margin-top: -20px;
}
.filterable .filters input[disabled] {
    background-color: transparent;
    border: none;
    cursor: auto;
    box-shadow: none;
    padding: 0;
    height: auto;
}
.filterable .filters input[disabled]::-webkit-input-placeholder {
    color: #333;
}
.filterable .filters input[disabled]::-moz-placeholder {
    color: #333;
}
.filterable .filters input[disabled]:-ms-input-placeholder {
    color: #333;
}


</style>

      <!-- Latest compiled and minified CSS
  -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">


    <!-- Optional theme
 -->
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
    <!-- Custom styles for this template -->
    <link href="http://getbootstrap.com/examples/navbar-static-top/navbar-static-top.css" rel="stylesheet">
<script>

function get(clicked_id)
{   var URL = "{% url 'data' %}";
    var data = {'rowd':clicked_id}

    $.post(URL, data, function(response){

    });

}


</script>

<html lang="en">
{% include "base.html" %}
{% load staticfiles %}
{% block content %}
<div class="parent" width="50px">  
<div class=" left ">
<img src="{% static "media/map_delhi.jpg" %}" />
<div id="mm" onclick='get(this.id);' style="position:absolute;cursor:pointer;z-index:2;height:20;width:20;left: 295; top:370;"><img src="{% static "media/active.gif" %}" /></div>
<div id="civilLines" onclick='get(this.id);' style="position:absolute;cursor:pointer;z-index:2;height:20;width:20;left: 329; top:300;"><img src="{% static "media/active.gif" %}" /></div>
<div id="rkPuram" onclick='get(this.id);' style="position:absolute;cursor:pointer;z-index:2;height:20;width:20;left: 265; top:480;"><img src="{% static "media/active.gif"%}" /></div>
<div id="pb" onclick='get(this.id);' style="position:absolute;cursor:pointer;z-index:2;height:20;width:20;left: 175; top:315;"><img src="{% static "media/active.gif" %}" /></div>
<div id="av" onclick='get(this.id);' style="position:absolute;cursor:pointer;z-index:2;height:20;width:20;left: 465; top:355;"><img src="{% static "media/active.gif" %}" /></div>
<div id="airpo" onclick='get(this.id);' style="position:absolute;cursor:pointer;z-index:2;height:20;width:20;left: 155; top:497;"><img src="{% static "media/active.gif"%}" /></div>
</div>
  <div class="right ">

    <div class="row">
        <div class="panel panel-primary filterable">
            <div class="panel-heading">
                <h3 class="panel-title">Air Quality</h3>
                <div class="pull-right">

                </div>
            </div>
            <table class="table">
                <thead>
                    <tr class="filters">

                        <th><input type="text" class="form-control" placeholder="Parameters" disabled></th>
                        <th><input type="text" class="form-control" placeholder="concentration" disabled></th>

                    </tr>
                </thead>

                <tbody>

                    <tr>
                        <td>Ammonia</td>
                        <td>{{ Ammonia }}</td>

                    </tr>
                    <tr>
                        <td>Carbon Monoxide</td>
                        <td>{{ Carbon_Monoxide }}</td>

                    </tr>
                    <tr>
                        <td>Nitrogen Oxide</td>
                        <td>{{ Nitrogen_Oxide }}</td>

                    </tr>
                    <tr>
                        <td>Ozone</td>
                        <td>{{ Ozone }}</td>

                    </tr>
                    <tr>
                        <td>Sulphur Dioxide</td>
                        <td>{{ Sulphur_Dioxide }}</td>

                    </tr>
                    <tr>
                        <td>Ambient Temperature</td>
                        <td>{{ Ambient_Temperature }}</td>

                    </tr>
                    <tr>
                        <td>Particulate Matter 10</td>
                        <td>{{ Particulate_Matter_10 }}</td>

                    </tr>
                    <tr>
                        <td>Benzene</td>
                        <td>{{ Benzene }}</td>

                    </tr>
                    <tr>
                        <td>Nitrogen Dioxide</td>
                        <td>{{ Nitrogen_Dioxide }}</td>

                    </tr>
                    <tr>
                        <td>Oxides of Nitrogen</td>
                        <td>{{ Oxides_of_Nitrogen }}</td>

                    </tr>
                    <tr>
                        <td>p-Xylene</td>
                        <td>{{ p_Xylene }}</td>

                    </tr>
                    <tr>
                        <td>Toluene</td>
                        <td>{{ Toluene }}</td>

                    </tr>
                    <tr>
                        <td>Barometric Pressure</td>
                        <td>{{ Barometric_Pressure }}</td>

                    </tr>
                    <tr>
                        <td>Particulate Matter 2</td>
                        <td>{{ Particulate_Matter_2 }}</td>

                    </tr>
                    <tr>
                        <td>Solar Radiation</td>
                        <td>{{ Solar_Radiation }}</td>
                </tbody>
            </table>
        </div>
    </div>
</div>

0 个答案:

没有答案