3

I'm receiving this error each time I try to access a list within a Django template. I have checked the answers to similar questions, but the problem is usually a lack of % or some other character somewhere. As long as I can see, this is not the case:

Here I'm passing a dict containing a a list of item id as keys and list of URLs to images as a value for each id. I know I should integrate this into the item model, but since I'm still at development with SQLite3 I cannot store lists easily. And anyway I am intrigued by this problem. So:

<a href="{% url 'details_view' item_id=item.id %}"><img class="hover-image" src="{{ img_gallery[item.id][0] }}" alt="">

Exception Value:    
Could not parse the remainder: '['item.id'][0]' from 'img_gallery['item.id'][0]'

Also, yesterday I tried using bootstrap4 flex-grid to easily achieve 5 columns. Since I'm using pagination to retrieve 20 items, my idea was slicing the list of items (model) for each row, like:

{% for item in items[0:5] %}

And I also received the same error, even when this was the recommended aproach in related answers aboput slicing data passed with a view.

In both cases I cannot find the problem, and I think both are somehow related.

I'm using latest Django 1.11.6 with Python 3.5.2.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
  • 2
    jinja2 != django templates; the two are related but Django does not use Jinja, the latter was originally inspired by the Django template syntax but is a separate project altogether. – Martijn Pieters Oct 19 '17 at 07:43
  • @MartijnPieters Nice to know, I always thought it was 100% jinja2. Is that the source of the error? Is that wrong syntax for Django-templates? – Roman Rdgz Oct 19 '17 at 07:49
  • Yes, see my answer below. – Martijn Pieters Oct 19 '17 at 07:58
  • 1
    You shouldn't add `get_item` to your `INSTALLED_APPS`. The `templatetags` directory is in your `shop` app, which is presumably already in `INSTALLED_APPS`. – Alasdair Oct 19 '17 at 08:51

1 Answers1

6

You seem to be confused between Jinja2 syntax and the Django template syntax. Jinja2 is a separate project, inspired by Django, but not used by Django itself.

In the Django template syntax, variables in {{...}} always use dot notation, [...] subscriptions are not supported. Out of the box, the language does not support dictionary key lookups.

You can write a custom filter to achieve this, like the following, written by culebrón:

from django.template.defaulttags import register

@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)

then in the template:

{{ img_gallery|get_item:item.id|first }}

Alternatively, you could switch to using Jinja2 in your Django project, replacing the built-in template language: How to use jinja2 as a templating engine in Django 1.8

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I'm following the instructions in https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/ but I'm still getting the error "No module named 'get_item'". I have updated the question to provide more data – Roman Rdgz Oct 19 '17 at 08:28
  • @RomanRdgz: from the accept I presume you have managed to resolve it? It looked to me like you had a `__init__.py` spelling mistake. At any rate, it'd be a new question, the issue with the syntax is resolved. – Martijn Pieters Oct 19 '17 at 09:23
  • @RomanRdgz: ah, I see the comment on the question; all you needed was to load the module in the template and be *part* of an already-registered app. – Martijn Pieters Oct 19 '17 at 09:29