RobinBattle
RobinBattle

Reputation: 228

access list by index in django template

I am trying to access an index position in list dynamically.

{{ allAppsList.{{app.id}}.link }}

It is not working.

{{ allAppsList.0.link }}

worked.

It should be some grammar issue. I google online, but didn't find anything helpful.

BTW: This is what I want to do if using in Java.

int index = app.id;

String link = appList[index].getLink();

Upvotes: 3

Views: 5571

Answers (2)

Alireza Savand
Alireza Savand

Reputation: 3488

You can't do such a thing in django template, But it's not impossible also.

If you are really into this, you can create a custom template filter. The implementation would be easy, Just pass the list and index to your custom filter and do exactly what you would do with Java.

Start to writing custom template filter

Upvotes: 2

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53981

You can't lookup lists like that in Django's template language - it's a design decision. Method calls, dictionary keys, attribute calls and list indexes are all handled by the . (dot) notation. Have a look at the docs for more about how Django decides on what to do when it encounters a dot

Upvotes: 0

Related Questions