title: How to loop through rows and populate columns using data from database in HTML and Django
body: <p>I am having a problem where the data I am trying to input into a table is not showing properly. I believe my queries are correct since they return the right information when I test them but when I try to print them to the table they print out wrong.</p>
  
  <p><a href="https://imgur.com/a/5u9jQW2" rel="nofollow noreferrer">https://imgur.com/a/5u9jQW2</a></p>
  
  <p>I tried messing around with different for loops, different types of html tags with no luck.</p>
  
  <pre><code>&lt;div class="table-responsive"&gt;
            &lt;table class="table align-items-center table-flush"&gt;
               &lt;thead class="thead-light"&gt;
                  &lt;tr&gt;
                     &lt;th scope="col"&gt; Student&lt;/th&gt;
                     {%  for Date in class_dates %}
  
                     &lt;th scope="col"&gt;{{Date.date}}&lt;/th&gt;
  
                     {% endfor %}
                     &lt;/thead&gt;
                    &lt;/tr&gt;
  
  
  
          &lt;tbody&gt;
            {% for students in all_students %} 
            &lt;tr&gt;
  
                &lt;th scope="row"&gt;
                  &lt;div class="media-body"&gt;
  
                    &lt;a href="/student/" class="mb-0 text-sm"&gt;{{students.student_name}} &lt;/a&gt;
  
  
                  &lt;/div&gt;
              &lt;/th&gt;
              &lt;td&gt;
             {%  for date in class_dates %}
  
  
                {% ifequal date.dates attendance.date %}
  
                {{students.status}}
              {% endifequal %}
             &lt;/td&gt;
             {% endfor %}  
             {% endfor %}
  
  
  
            &lt;/tr&gt;
          &lt;/tbody&gt;
  
  
  
          &lt;/table&gt; 
  </code></pre>
  
  <p>It is only printing the dates for april 7th over and over again for all the other dates. Since there is only two students, it seems like its only grabbing one date for each student. Each other date should have their own status. The content the queries print is correct.</p>
  
  <p>Update: heres the queries</p>
  
  <pre><code>class_dates = Class.objects.filter(course=301)
  
  all_students = Student.objects.select_related().raw('SELECT * '
                                                          'FROM students as S, classes as CL, attendances as A '
                                                          'WHERE CL.course_id = 301 AND S.student_id = A.students '
                                                          'AND CL.course_id = A.classes AND CL.date = A.date '
                                                          'GROUP BY student_name'
                                                           )
  status = Student.objects.select_related().raw('SELECT   A.*'
                                                          'FROM students as S, classes as CL, attendances as A '
                                                          'WHERE CL.course_id = 301 AND CL.course_id = A.classes '
                                                          'AND CL.date = A.date '
                                                          'GROUP BY A.id'
                                                          'ORDER BY A.students'
                                                          )   
  </code></pre>
  
  <p>all_students is what lists the students, and all class_dates is what lists the dates in header. Status is what is supposed be filled into the table.</p>