在页面刷新时从django视图调用python.py文件

时间:2016-08-06 13:38:59

标签: python django

我已经准备好了一个python文件,每次页面刷新时我都会尝试简单地运行它。

Django说我不能将导入放在def home()部分。

import weatherapi在某些时候确实有效,但如果我去刷新页面,它就不会运行weatherapi.py文件中的所有任务。

views.py

from django.shortcuts import render, render_to_response, RequestContext, HttpResponseRedirect
from weatherapi import *
import weatherapi

def home(request):
    cnxn = pyodbc.connect(r'DRIVER={SQL Server};)
    cursor = cnxn.cursor()
    cursor.execute("select top 1 * from [weatherdb].[dbo].[FactWeather] ORDER BY ID DESC")
    rows = cursor.fetchall()
    for row in rows:
        headtitle = row.ID
        rec_time = row.rec_time
        ref_time = row.ref_time
        wind_speed_km = row.wind_speed_km
        wind_degree = row.wind_degree
        humidity = row.humidity
        temp = row.temp
        dewpoint = row.dewpoint
        cloud_cov = row.cloud_cov
        rain_vol = row.rain_vol
        atmo_press = row.atmo_press
        status_short = row.status_short
        status_detail = row.status_detail
        sunrise_time = row.sunrise_time
        sunset_time = row.sunset_time


        return render(request, "home.html", 
            {"headtitle": headtitle,
            "rec_time": rec_time ,
            "ref_time": ref_time ,
            "wind_speed_km": wind_speed_km ,
            "wind_degree": wind_degree ,
            "humidity": humidity ,
            "temp": temp ,
            "dewpoint": dewpoint ,
            "cloud_cov": cloud_cov ,
            "rain_vol": rain_vol ,
            "atmo_press": atmo_press ,
            "status_short": status_short ,
            "status_detail": status_detail ,
            "sunrise_time": sunrise_time ,
            "sunset_time": sunset_time,
            })

weatherapi.py

import pyowm
import datetime
import time
import pyodbc

rec_time = datetime.datetime.now()

owm = pyowm.OWM('removed')  # You MUST provide a valid API key

observation = owm.weather_at_id(2158177) 
w = observation.get_weather()

# Weather details
ref_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(w.get_reference_time()))
wind_speed_km = ((w.get_wind().get('speed')*60)*60)/1000
wind_degree = w.get_wind().get('deg') 
humidity = w.get_humidity()              # 87 humidity percentage
temp = w.get_temperature('celsius').get('temp')  # {'temp_max': 10.5, 'temp': 9.7, 'temp_min': 9.0}
dewpoint = temp - ((100 - humidity)/5.)
cloud_cov = w.get_clouds()                # Get cloud coverage
rain_vol = w.get_rain().get('3h')                  # Get rain volume
atmo_press = w.get_pressure().get('press')              # atmospheric pressure
status_short = w.get_status()                # Get weather short status
status_detail = w.get_detailed_status()       # Get detailed weather status
sunrise_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(w.get_sunrise_time()))         # Sunrise time (GMT UNIXtime or ISO 8601)
sunset_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(w.get_sunset_time()))    # Sunset time (GMT UNIXtime or ISO 8601)

### Fix rain volume empty dict
if ((rain_vol == {}) == True):
    rain_vol = 0
else:
    rain_vol



###Write to DB

cnxn = pyodbc.connect(r'DRIVER={SQL Server};')
cursor = cnxn.cursor()


tvar = (rec_time, ref_time ,
wind_speed_km ,
wind_degree ,
humidity ,
temp ,
dewpoint, 
cloud_cov, 
rain_vol ,
atmo_press, 
status_short, 
status_detail, 
sunrise_time ,
sunset_time)

cursor.execute("""insert into [weatherdb].[dbo].[FactWeather]
    (rec_time, ref_time ,
wind_speed_km ,
wind_degree ,
humidity ,
temp ,
dewpoint, 
cloud_cov, 
rain_vol ,
atmo_press, 
status_short, 
status_detail, 
sunrise_time ,
sunset_time
     ) 

    values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", tvar )
cnxn.commit()


# print( rec_time, ref_time ,
# wind_speed_km ,
# wind_degree ,
# humidity ,
# temp ,
# dewpoint, 
# cloud_cov, 
# rain_vol ,
# atmo_press, 
# status_short, 
# status_detail, 
# sunrise_time ,
# sunset_time ,
# )

0 个答案:

没有答案