views:

38

answers:

1

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" classes.

How can I control the output/formatting so that the brackets and class name don't appear, and only specific fields. ?

+4  A: 

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.

T. Stone
Awesome. d seems to be an arbitrary specification. Am I correct ?
Michael Moreno
Uhm, `enumerables` => `iterable`, n'est-ce pas?
Peter Rowell
@michael -- yes, that's just a variable name that you can pick.
T. Stone
@peter -- ah that's right python calls them iterable (other languages call the same data type enumerable)
T. Stone