为了账号安全,请及时绑定邮箱和手机立即绑定

通过其他循环循环

通过其他循环循环

慕慕森 2022-08-25 13:39:05
循环通过其他循环,我正在尝试从上下文中插入数据。在这种情况下,我该如何避免循环其他循环。我该如何修复它?views.py我正在使用beautifulsoup从wiki中获取数据。正在上传图像。它返回以下内容:模板.html{%extends 'profilecontent/base.html' %}{%block content%}    <div class="container-home">        <div class="home-wrapper home-wrapper-first">            <p class='home-matches'>Przyszłe mecze <span class='home-week'>W3 D2</span></p>                <div class="match-wrapper">                    <table>                        {%for second_team in second_team_names%}                            {%for first_team in first_team_names %}                        {%for hour_game in hours_games%}                            <tr><td>{{first_team}}</td><td>{{hour_game}}</td><td>{{second_team}}</td></tr>                            {%endfor%}                        {%endfor%}                        {%endfor%}                    </table>                             </div>        </div>        <div class="home-wrapper home-wrapper-second">        </div>    </div>{%endblock%}views.pyfrom bs4 import BeautifulSoupimport requestsdef home(request):    source = requests.get('https://lol.gamepedia.com/Ultraliga/Season_3').text    soup = BeautifulSoup(source, 'lxml')    first_team_names = []    second_team_names = []    td1s = None    td2s = None    hours_games = ['17:30', '18:30', '19:30', '20:30']    tables = soup.find_all('table', class_='wikitable matchlist')    for table in tables:        td1s = table.find_all('td', class_='matchlist-team1')        td2s = table.find_all('td', class_='matchlist-team2')    for td in td1s:        span = td.find('span')        first_team_names.append(span.text)    for td in td2s:        span = td.find('span')        second_team_names.append(span.text)    context = {        'first_team_names':first_team_names,        'second_team_names':second_team_names,        'hours_games':hours_games,        }    return render(request, 'profilecontent/home.html', context)
查看完整描述

1 回答

?
森栏

TA贡献1810条经验 获得超5个赞

您必须将三个列表放在视图中,然后将其发送到模板。然后同时在那里迭代它,如下所示...zip


def home(request):


    ...Your Logic...


    context = { "all_teams": zip(first_team_names, second_team_names, hours_games)}


    return render(request, 'profilecontent/home.html', context)

现在,解压缩模板并同时对其进行迭代...


{%extends 'profilecontent/base.html' %}

{%block content%}

    <div class="container-home">

        <div class="home-wrapper home-wrapper-first">

            <p class='home-matches'>Przyszłe mecze <span class='home-week'>W3 D2</span></p>

                <div class="match-wrapper">

                    <table>

                        {% for data in all_teams %}


                            <tr><td>{{ data.0.first_team }}</td><td>{{ data.2.hour_game }}</td><td>{{ data.1.second_team }}</td></tr>    


                        {%endfor%}

                    </table>             

                </div>

        </div>

        <div class="home-wrapper home-wrapper-second">


        </div>

    </div>

{%endblock%}


查看完整回答
反对 回复 2022-08-25
  • 1 回答
  • 0 关注
  • 86 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信