Я новичок в Django и не очень разбираюсь в представлении на основе классов. Я пытался вернуть значение в шаблоне Django, и я не смог получить доступ к этому значению с помощью тега шаблона. Я пытался напечатать значение расстояния и длительности, которые вы видите на моем графике. html код. Вот мой код views.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
мой код url.py
urlpatterns = [
re_path(r'chart.html/$',views.chartView.as_view(),name="Charts"),
]
urlpatterns += staticfiles_urlpatterns()
диаграмма. html код
{% 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 %}
и моя ошибка
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '[0]['duration']' from 'dist_list[0]['duration']'
[19/Feb/2020 00:32:22] "GET /chart.html/ HTTP/1.1" 500 159883