Controlling Display of Methods In Template
I have the following in a view (foreign key relationships via _set):
srvr = Server.objects.get(name=q)
return render_to_response('search_results.html',
{'drv': srvr.drive_set.all(), 'memry': srvr.memory_set.all(), 'query': q})
The results template includes:
{% if drv %}
<table>
<tr>
<td>{{ drv }}</td>
</tr>
</table>
{% endif %}
{% if memry %}
<li>{{ memry }}</li>
{% endif %}
The output looks like this:
[<Drive: IBM IBM-500 1111111 500Gb SATA>, <Drive: IBM IBM-500 2222222 500Gb SATA]
[<Memory: Samsung 512>, <Memory: Samsung 512>, <Memory: Samsung 512>]
I know this the result of the "unicode()" method in the "Drive" and "Memory" classe开发者_JAVA技巧s.
How can I control the output/formatting so that the brackets and class name don't appear, and only specific fields. ?
drv
and memry
are going to be iterable, and you can move through them with the for
tag...
{% if drv %}
<table>
{% for d in drv %}
<tr>
<td>{{ d.name }}</td><td>{{ d.size }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
The .name
and .size
are properties of whatever Model d
represents. Fill this in with whatever details actually exist that you're looking to render.
精彩评论