Junchao Gu
Junchao Gu

Reputation: 1865

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 %}
<tr>
    <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>
    <td>
        {{ tags_dict[unicode(unit)] }}
    </td>
    <td>{{ unit.last_modified|date:'Y-m-d' }} {{ unit.last_modified|time:'H:i:s' }}</td>
</tr>
{% endfor %}

But I got this error:

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

Upvotes: 0

Views: 3104

Answers (2)

Juan Medina
Juan Medina

Reputation: 142

Just remove the blank spaces like this:

<td>{{unit.last_modified|date:'Y-m-d'}}{{unit.last_modified|time:'H:i:s'}}</td>

Upvotes: 1

Jed Fox
Jed Fox

Reputation: 3025

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 }}.

Upvotes: 1

Related Questions