user7693832
user7693832

Reputation: 6849

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

Upvotes: 0

Views: 30

Answers (2)

Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13723

The reason is simple: a typo!

In you database, the field is called business_id and in your template it is called business_ip.

Try this:

 {{ row.business_id }}

Hope it helps!

Upvotes: 0

Exprator
Exprator

Reputation: 27503

<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)

Upvotes: 0

Related Questions