简体   繁体   中英

Need to run 2 Loops in the same statement in Flask

This is the Actual Output that I am getting. I need it to look like this . This is the code I'm using to show the "bunkable" value but I'm not getting proper output. Bunkable is a list and the datas is a list of tuples from SQLite. Any suggestions on how I can fix this or do I need to give more context/information?

        <div class='table-responsive-sm'>
      <table class='table table-bordered table-striped '>
        <thead style="background-color: #007ac6; color:aliceblue;" class="text-center">
          <th>CourseID</th>
          <th>Subject</th>
          <th>Bunked</th>
          <th>Remaining Hours</th>
        </thead>
        <tbody>
          {% for row in datas%}
            <tr>
              <td>{{row.CourseID}}</td>
              <td>{{row.Subject}}</td>
              <td>{{row.bunkedHours}}</td>
          {% endfor %}
          {% for bunkable in bunk %}
              <td>{{bunkable}}</td>
            </tr>
          {%endfor%}
        </tbody>
      </table>
    </div>

I haven't tried it, but could you do something like this

{% for row in datas%}
       <tr>
         <td>{{row.CourseID}}</td>
         <td>{{row.Subject}}</td>
         <td>{{row.bunkedHours}}</td>
         <td>{{bunk[loop.index0]}}</td>
       </tr>
{% endfor %} 

I am getting this from this related question, but it should work by getting the same index from the bunk list, which seems to be what you need to do.

So give that a try and let me know, as long as they are the same length lists always I think it should. Sorry if I am missing something, because I haven't had time to reproduce it and try this yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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