# Iterate model values in html table

**URL:** <https://forum.djangoproject.com/t/iterate-model-values-in-html-table/30673>\
**Category:** Getting Started\
**Created:** [April 30, 2024, 9:04am UTC](https://forum.djangoproject.com/t/iterate-model-values-in-html-table/30673 "2024-04-30T09:04:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hardip84](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hardip84/32/20960_2.png) [@hardip84](https://forum.djangoproject.com/u/hardip84)\
**Post date:** [April 30, 2024, 9:04am UTC](https://forum.djangoproject.com/t/iterate-model-values-in-html-table/30673/1 "2024-04-30T09:04:39Z")

</div>

Hi all,  
I have a html template with Student table. I want to iterate all Student details in html table with for loop.

This is my HTML file,

```auto
 <table>
              <tr style="color: #7abaff">
                  <th>ID</th>
                  <th>Student Name</th>
                  <th>Grade</th>
                  <th>Contact No</th>
                  <th>Address</th>
              </tr>
              {% for i in students %}
              <tr>
                  <td>01</td>
                  <td>{{ i.fname }}</td>
                  <td>12</td>
                  <td>9898989898</td>
                  <td>Ahmedabad</td>
              </tr>
              {% endfor %}
          </table>

```

and this is my views.py code,

```auto
def facultyindex(request):
    students = Student.objects.all()
    return render(request, 'facultyindex.html', {'students':students})

```

and last below is my Student Model

```auto
class Student(models.Model):
    fname = models.CharField(max_length=100)
    lname = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    password = models.CharField(max_length=100)
    contactno = models.CharField(max_length=100)
    address = models.TextField()
    grade = models.CharField(max_length=100)
    enrollmentstartdate = models.DateField()
    enrollmentenddate = models.DateField()
    daysremaining = models.IntegerField(default=365)
    feespaid = models.DecimalField(max_digits=8, decimal_places=2)
    paymentdate = models.DateField()

    def __str__ (self):
        return self.fname + "-" + self.lname

```

please help

Thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 30, 2024, 10:53am UTC](https://forum.djangoproject.com/t/iterate-model-values-in-html-table/30673/2 "2024-04-30T10:53:17Z")

</div>

Welcome @hardip84 !

It looks like you’re off to a really good start here. What seems to be the issue?

You’re already rendering the `fname` field, you should be able to add the other fields you need - unless there’s something not working at this point, in which case we’ll need to know what’s going wrong.

---

<div class="post-metadata">

**Author:** ![hardip84](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hardip84/32/20960_2.png) [@hardip84](https://forum.djangoproject.com/u/hardip84)\
**Post date:** [April 30, 2024, 11:17am UTC](https://forum.djangoproject.com/t/iterate-model-values-in-html-table/30673/3 "2024-04-30T11:17:45Z")

</div>

Thanks for the reply, i am new to django. I’ve tried all the possible solution but its not working. can u help me further ?

Thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 30, 2024, 11:36am UTC](https://forum.djangoproject.com/t/iterate-model-values-in-html-table/30673/4 "2024-04-30T11:36:52Z")

</div>

Possible solution for what?

You haven’t yet described what the problem or issue is that you’re facing.

What are you seeing that you don’t expect to be seeing? Or, what are you not seeing that you expect to see?

---

<div class="post-metadata">

**Author:** ![hardip84](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hardip84/32/20960_2.png) [@hardip84](https://forum.djangoproject.com/u/hardip84)\
**Post date:** [May 1, 2024, 8:06am UTC](https://forum.djangoproject.com/t/iterate-model-values-in-html-table/30673/5 "2024-05-01T08:06:47Z")

</div>

i am not able to get all student list in table using for loop. Whenever i try to run the code. the table values become empty.  
what i am doing wrong in this code ? pls help

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 1, 2024, 11:41am UTC](https://forum.djangoproject.com/t/iterate-model-values-in-html-table/30673/6 "2024-05-01T11:41:58Z")

</div>

So you are saying that the table is being rendered, but the `<td>{{ i.fname }}</td>` is rendering as `<td></td>`? (Are all the constant values are there and repeated?)

Is that html fragment at the top part of the `facultyindex.html` template?

How many `Student` do you have? Are you seeing that many table rows being rendered?

---

<div class="post-metadata">

**Author:** ![hardip84](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hardip84/32/20960_2.png) [@hardip84](https://forum.djangoproject.com/u/hardip84)\
**Post date:** [May 3, 2024, 10:24am UTC](https://forum.djangoproject.com/t/iterate-model-values-in-html-table/30673/7 "2024-05-03T10:24:25Z")

</div>

Sorry for the late reply. But it seems like there was some problem with my HTML template. i have used another template and it works fine. Thanks
