django

The foreign key has value, but do not shows up in template


This code is the templates html:

<table border="1">
    <thead>
    <tr>
        <th>1</th>
        <th>1</th>
        <th>1</th>
        <th>1</th>
        <th>1</th>
        <th>1</th>
    </tr>
    </thead>
    <tbody>
    {% for row in hosts %}
    <tr>
        <td>{{row.nid}}</td>
        <td>{{row.hostname}}</td>
        <td>{{row.ip}}</td>
        <td>{{row.port}}</td>
        <td>{{row.business_ip}}</td>
        <td>{{row.business.caption}}</td>
    </tr>
    {% endfor %}
    </tbody>
</table>

business_ip is a foreign key of the host table, and in the sqlite3 it has value:

enter image description here

But the row.business_ip did not shows up, nor the row.business.ip in the browser:

enter image description here


Solution

  • <td>{{row.business_id}}</td>
    

    should be the name as that's what defined in the database

    or

    <td>{{ row.business.id }}</td>
    

    as it's a foreign key and I hope you have declared as

    business = models.ForeignKey(modelname)