通过其他循环循环

循环通过其他循环,我正在尝试从上下文中插入数据。在这种情况下,我该如何避免循环其他循环。我该如何修复它?views.py

我正在使用beautifulsoup从wiki中获取数据。正在上传图像。

它返回以下内容:

http://img2.mukewang.com/63070b1f0001983f05090929.jpg

模板.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.py


from bs4 import BeautifulSoup

import requests




def 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)

http://img2.mukewang.com/63070b2e0001af1c04850929.jpghttp://img4.mukewang.com/63070b310001af1c04850929.jpg

慕慕森
浏览 87回答 1
1回答

森栏

您必须将三个列表放在视图中,然后将其发送到模板。然后同时在那里迭代它,如下所示...zipdef home(request):&nbsp; &nbsp; ...Your Logic...&nbsp; &nbsp; context = { "all_teams": zip(first_team_names, second_team_names, hours_games)}&nbsp; &nbsp; return render(request, 'profilecontent/home.html', context)现在,解压缩模板并同时对其进行迭代...{%extends 'profilecontent/base.html' %}{%block content%}&nbsp; &nbsp; <div class="container-home">&nbsp; &nbsp; &nbsp; &nbsp; <div class="home-wrapper home-wrapper-first">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class='home-matches'>Przyszłe mecze <span class='home-week'>W3 D2</span></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="match-wrapper">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% for data in all_teams %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr><td>{{ data.0.first_team }}</td><td>{{ data.2.hour_game }}</td><td>{{ data.1.second_team }}</td></tr>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {%endfor%}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="home-wrapper home-wrapper-second">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>{%endblock%}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python