1

I have one for loop. And inside i want to access to other variable (array) by foorloop.counter0. How? I have tried like this: {{otherVariable.foorloop.counter0}} but nothing.

And like this works: {{otherVariable.0}}

3
  • what is exactly that you need that for? as: can you give us a bit more of context on what you're trying to accomplish, so maybe we can help you figure out a better approach for it? Commented Sep 11, 2013 at 16:38
  • Your question is rather confusing, please add code samples. Commented Sep 11, 2013 at 16:43
  • i have two arrays and they have the same number of elements. I want to iterate through one array and get corresponding element from another.. Commented Sep 11, 2013 at 16:52

1 Answer 1

1

You can use a custom template filter (http://djangosnippets.org/snippets/2740/) like

{{ otherVariable|get_at_index:forloop.counter0 }

or use the built-in 'slice' template tag (https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice)

{{otherVariable|slice forloop.counter0 }}

How to define custom templatetags/filters

Reference - https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#code-layout

Your custom tags and filters will live in a module inside the templatetags directory, say in a file named 'custom_utils/py'

And in your template you would use the following: {% load custom_utils %}

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! just one thing where to define that tag? in what file?
np, Updated answer with that info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.