I have a database object called manor_stats
, with around 30 fields. For most rows, most of these fields will be null.
In my template, I'd like to loop through all the fields in the row, and print info for only the fields that aren't null.
For example, there's a field called "name": I'd like to print <li>Name: {{ manor_stats.name }}</li>
in the template ONLY for those objects where the field isn't null. Ideally I'd like to pull in "Name: " from somewhere automatically too, rather than specifying it.
I know I could use {% if manor_stats.name %}
to check whether each field is null, but I don't want to do that 30 times for all the fields.
Here's what I have in views.py:
manor_stats = Manors.objects.get(idx=id)
return render_to_response('place.html', { 'place' : place, 'manor_stats' : manor_stats }, context_instance = RequestContext(request))
And then in place.html, I'd like to have something that works approximately like this (pseudocode, with ??? indicating the bits that I don't know how to do):
{% if manor_stats %}
<ul>
{% for manor_stats.property??? in manor_stats %}
{% if manor_stats.property %}
<li>{{ manor_stats.property.field_name??? }} {{ manor_stats.property.value??? }}</li>
{% endif %}
{% endfor %
{% endif %}
Hope that makes sense...