Python 无法使用基于类的视图在django模板中返回值
我是Django新手,对基于类的视图知之甚少。我试图返回Django模板中的值,但无法使用模板标记访问这些值。我试图打印可以在我的chart.html代码中看到的距离和持续时间的值。 这是我的views.py代码Python 无法使用基于类的视图在django模板中返回值,python,html,django,Python,Html,Django,我是Django新手,对基于类的视图知之甚少。我试图返回Django模板中的值,但无法使用模板标记访问这些值。我试图打印可以在我的chart.html代码中看到的距离和持续时间的值。 这是我的views.py代码 class chartView(TemplateView): template_name = 'chart.html' def calculate(self): self.res=requests.get('https://ipinfo.io/')
class chartView(TemplateView):
template_name = 'chart.html'
def calculate(self):
self.res=requests.get('https://ipinfo.io/')
self.data=self.res.json()
self.current_loc=self.data['city']
gmaps = googlemaps.Client(key='****************************')
my_dist = gmaps.distance_matrix(self.current_loc,'bhaktapur')['rows'][0]['elements'][0]
# Printing the result
#the variable name describes the distance and time for that bloodbank eg:-redcross,whitecross etc.
redcross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
my_dist = gmaps.distance_matrix(self.current_loc,'lalitpur')['rows'][0]['elements'][0]
whitecross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
my_dist = gmaps.distance_matrix(self.current_loc,'jorpati')['rows'][0]['elements'][0]
greencross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
my_dist = gmaps.distance_matrix(self.current_loc,'maharajgunj')['rows'][0]['elements'][0]
yellowcross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
dist_list=[redcross,whitecross,greencross,yellowcross]
return render(request,'chart.html',{'dist_list': dist_list})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["qs"]= app.objects.all()
return context
urlpatterns = [
re_path(r'chart.html/$',views.chartView.as_view(),name="Charts"),
]
urlpatterns += staticfiles_urlpatterns()
{% for items in qs %}
<div class="conty">
<canvas class="myChart"></canvas>
</div><br><br>
<div align="center">
<table>
<tr>
<th>{{items.name}}</th>
<th>{{dist_list[0]['distance']}}</th>
<th>{{dist_list[0]['duration']}}</th>
</tr>
</table>
</div>
{% endfor %}
我的url.py代码
class chartView(TemplateView):
template_name = 'chart.html'
def calculate(self):
self.res=requests.get('https://ipinfo.io/')
self.data=self.res.json()
self.current_loc=self.data['city']
gmaps = googlemaps.Client(key='****************************')
my_dist = gmaps.distance_matrix(self.current_loc,'bhaktapur')['rows'][0]['elements'][0]
# Printing the result
#the variable name describes the distance and time for that bloodbank eg:-redcross,whitecross etc.
redcross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
my_dist = gmaps.distance_matrix(self.current_loc,'lalitpur')['rows'][0]['elements'][0]
whitecross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
my_dist = gmaps.distance_matrix(self.current_loc,'jorpati')['rows'][0]['elements'][0]
greencross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
my_dist = gmaps.distance_matrix(self.current_loc,'maharajgunj')['rows'][0]['elements'][0]
yellowcross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
dist_list=[redcross,whitecross,greencross,yellowcross]
return render(request,'chart.html',{'dist_list': dist_list})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["qs"]= app.objects.all()
return context
urlpatterns = [
re_path(r'chart.html/$',views.chartView.as_view(),name="Charts"),
]
urlpatterns += staticfiles_urlpatterns()
{% for items in qs %}
<div class="conty">
<canvas class="myChart"></canvas>
</div><br><br>
<div align="center">
<table>
<tr>
<th>{{items.name}}</th>
<th>{{dist_list[0]['distance']}}</th>
<th>{{dist_list[0]['duration']}}</th>
</tr>
</table>
</div>
{% endfor %}
chart.html代码
class chartView(TemplateView):
template_name = 'chart.html'
def calculate(self):
self.res=requests.get('https://ipinfo.io/')
self.data=self.res.json()
self.current_loc=self.data['city']
gmaps = googlemaps.Client(key='****************************')
my_dist = gmaps.distance_matrix(self.current_loc,'bhaktapur')['rows'][0]['elements'][0]
# Printing the result
#the variable name describes the distance and time for that bloodbank eg:-redcross,whitecross etc.
redcross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
my_dist = gmaps.distance_matrix(self.current_loc,'lalitpur')['rows'][0]['elements'][0]
whitecross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
my_dist = gmaps.distance_matrix(self.current_loc,'jorpati')['rows'][0]['elements'][0]
greencross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
my_dist = gmaps.distance_matrix(self.current_loc,'maharajgunj')['rows'][0]['elements'][0]
yellowcross={
'distance':my_dist['distance']['text'],
'duration':my_dist['duration']['text']
}
dist_list=[redcross,whitecross,greencross,yellowcross]
return render(request,'chart.html',{'dist_list': dist_list})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["qs"]= app.objects.all()
return context
urlpatterns = [
re_path(r'chart.html/$',views.chartView.as_view(),name="Charts"),
]
urlpatterns += staticfiles_urlpatterns()
{% for items in qs %}
<div class="conty">
<canvas class="myChart"></canvas>
</div><br><br>
<div align="center">
<table>
<tr>
<th>{{items.name}}</th>
<th>{{dist_list[0]['distance']}}</th>
<th>{{dist_list[0]['duration']}}</th>
</tr>
</table>
</div>
{% endfor %}
views.py中的my函数是否从
def calculate(self)
返回值?