Junchao Gu Junchao Gu - 3 months ago 51
Python Question

Django template error: could not parse the remainder

I am trying to render a value from a dict in the Django(1.6.11) template. unit_list is list of unit(model). unit.unit_id is the primary key of unit. tags_dict is dict of tags with unit_ids as keys and tags as values.

{% for unit in unit_list %}
<td>{{ unit.unit_id }}</td>
<td>{{ unit.version }}</td>
<td>{{ unit.release_dt|date:'Y-m-d' }} {{ unit.release_dt|time:'H:i:s' }}</td>
<td>{{ unit.update_dt|date:'Y-m-d' }} {{ unit.update_dt|time:'H:i:s' }}</td>
{{ tags_dict[unicode(unit)] }}
<td>{{ unit.last_modified|date:'Y-m-d' }} {{ unit.last_modified|time:'H:i:s' }}</td>
{% endfor %}

But I got this error:

Could not parse the remainder: '(unicode(unit))' from 'tags_dict.get(unicode(unit))'


This is because you can’t put function calls like this into a Django template. It looks like you should do this in your view and pass it to the template as a variable. You could also add a method on the unit class, like this:

def get_tags(self):
    tags_dict = {} # TODO: retrieve tags dict.
    return tags_dict[unicode(self)]

Then, you could do something like this in your template: {{ unit.get_tags }}.