在django中view.py怎么写调用test.py代码实现在index.html显示出来
课程设计
1
test.py代码如下
def test():
import requests
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
url1 = 'http://push2.eastmoney.com/api/qt/stock/get?secid=133.USDCNH&ut=bd1d9ddb04089700cf9c27f6f7426281&fields=f532,f57,f58,f59,f107,f43,f44,f45,f46,f60,f152,f169,f170,f119,f120,f121,f122&invt=2&cb=jQuery112408944986601278446_1618535639304&_=1618535639305'
jsonData = requests.get(url1)
data = jsonData.text
aaausa = data.split('jQuery112408944986601278446_1618535639304(')[1].split(')')[0]
result_lis11 = aaausa.split(",")
x = ('%.4f' % (int(result_lis11[19].split(":")[1]) * 0.0001)) # 涨跌
y = (int(result_lis11[20].split(":")[1]) * 0.01) # 涨幅
z = ('%.4f' % (int(result_lis11[21].split(":")[1]) * 0.0001)) # 卖出汇率
list1 = print(z)
return list1
test()
view.py代码如下:
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
import datetime
now =datetime.datetime.now()
return render(request,'index.html',{"time": now})
def index(request):
# return render(request, 'index.html', locals())
list = ["1","2","3"]
return render(request, 'index.html', {"list": list})
-
test.py代码如下 def test(): import requests header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'} url1 = 'http://push2.eastmoney.com/api/qt/stock/get?secid=133.USDCNH&ut=bd1d9ddb04089700cf9c27f6f7426281&fields=f532,f57,f58,f59,f107,f43,f44,f45,f46,f60,f152,f169,f170,f119,f120,f121,f122&invt=2&cb=jQuery112408944986601278446_1618535639304&_=1618535639305' jsonData = requests.get(url1) data = jsonData.text aaausa = data.split('jQuery112408944986601278446_1618535639304(')[1].split(')')[0] result_lis11 = aaausa.split(",") x = ('%.4f' % (int(result_lis11[19].split(":")[1]) * 0.0001)) # 涨跌 y = (int(result_lis11[20].split(":")[1]) * 0.01) # 涨幅 z = ('%.4f' % (int(result_lis11[21].split(":")[1]) * 0.0001)) # 卖出汇率 list1 = [str(x),str(y),str(z)] return list1 view.py代码如下: from django.http import HttpResponse from django.shortcuts import render def index(request): import datetime from test import test now =datetime.datetime.now() list = test() return render(request,'index.html',{"time": now,"list": list})
-
这样试试 分别修改 test.py , view.py 把 test.py 放在 django 项目目录下(就是比views.py 那个目录高一层)。
发表回复