问题描述
我一直在尝试使用此Python运动数据API Sportsipy.设置非常简单,我可以将数据保存到var并将其打印到终端,但是当我将其添加到上下文时,请通过我的Django模板进行传递.
I have been trying to work with this Python sports data api, Sportsipy. It’s pretty simple to set up and I can save data to a var and print it to the terminal but when I add it to context go to pass it through my Django template nothing shows up.
我尝试用几种不同的方式在HTML端调用它,但我仍然无法弄清楚.
I tried calling it on the HTML side several different ways but I still haven't been able to figure it out.
在Schedule端点下 https://sportsreference.readthedocs.io/en/stable/ncaab.html#module-sportsipy.ncaab.boxscore
Under the Schedule endpointhttps://sportsreference.readthedocs.io/en/stable/ncaab.html#module-sportsipy.ncaab.boxscore
def games(request):
""" View to return games page """
team_schedule = Schedule('PURDUE')
print(team_schedule)
for game in team_schedule:
away_total_rebounds = game.boxscore.away_total_rebounds
print(away_total_rebounds)
context = {
'team_schedule': team_schedule,
'away_total_rebounds': away_total_rebounds,
}
return render(request, 'games/games.html', context)
{% for game in team_schedule %}
<div>
<h4 class="white">{{ game.boxscore.away_total_rebounds }}</h4>
</div>
{% endfor %}
推荐答案
我不确定这是否是正确的答案,但是您的for循环是否不应该包含for循环的元素?我要说的是模板中的内容
I'm not sure if it is the correct answer but shouldn't your for loop have elements of the for loop? What I'm trying to say is in the template shouldn't it be like
{% for game in team_schedule %}
<div>
<h4 class="white">{{ game.boxscore.away_total_rebounds }}</h4>
</div>
{% endfor %}
编辑:添加了阿卜杜勒的答案
EDIT: Abdul's answer added
这篇关于如何将变量从Python API传递到Django模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!