简体   繁体   English

如何将变量从 Python API 传递到 Django 模板

[英]How to pass variable from Python API to Django template

I have been trying to work with this Python sports data api, Sportsipy.我一直在尝试使用这个 Python 体育数据 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.设置非常简单,我可以将数据保存到 var 并将其打印到终端,但是当我将其添加到上下文 go 以通过我的 Django 模板传递它时,什么也没有显示。

I tried calling it on the HTML side several different ways but I still haven't been able to figure it out.我尝试用几种不同的方式在 HTML 端调用它,但我仍然无法弄清楚。

Screenshot of API endpoint doc API 端点文档的屏幕截图

Under the Schedule endpoint https://sportsreference.readthedocs.io/en/stable/ncaab.html#module-sportsipy.ncaab.boxscore在 Schedule 端点https://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 %} 

I'm not sure if it is the correct answer but shouldn't your for loop have elements of the for loop?我不确定这是否是正确的答案,但你的 for 循环不应该有 for 循环的元素吗? 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编辑:添加了阿卜杜勒的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM