Search code examples
pythondjangodjango-templatesjinja2

Getting Error in Jinja using in Django Project


I am Facing an error in jinja while working on tables.

I have a model name HOD which uses user as foreign key and contains other details about Hod such as mobile no, department, year, etc.

Now I have a table in my html code in which I used jinja to load all hod details in the table using jinja for loop but I am unable to get first name and last name of hod.

This is my code:

<table class="table">
 <thead>
  <tr>
    <th scope="col">#</th>
    <th scope="col">First</th>
    <th scope="col">Last</th>
    <th scope="col">Department</th>
    <th scope="col">Year</th>
    <th scope="col">Mobile No</th>
  </tr>
</thead>
<tbody>
    {%for hod in hods%}
  <tr>
    <th scope="row">{{hod.id}}</th>
    <td>{{user.hod.first_name}}</td>
    <td>{{user.hod.last_name}}</td>
    <td>{{hod.hod_of_department}}</td>
    <td>{{hod.hod_of_year}}</td>
    <td>{{hod.mobile_no}}</td>
  </tr>
  {%endfor%}
</tbody>

Solution

  • You are trying to get firstname, lastname from the user which is not there:

    {% for hod in hods %}
      <tr>
        <th scope="row">{{hod.id}}</th>
        <td>{{hod.first_name}}</td>
        <td>{{hod.last_name}}</td>
        <td>{{hod.hod_of_department}}</td>
        <td>{{hod.hod_of_year}}</td>
        <td>{{hod.mobile_no}}</td>
      </tr>
      {% endfor %}