How can I access part of a tuple which is returned from a custom tag in a Django template?

StackOverflow https://stackoverflow.com/questions/17032547

Question

I have a function that returns a non-current user's first and last name as a tuple, using the following custom tag, where foo is a username from active directory:

{% name foo %}

I would like to access the first element of the tuple, and the only syntax I could think of is:

{{ {% name foo %}.0 }}

which is incorrect. How can I accomplish this?

Était-ce utile?

La solution

You can't do that. If your tag is simply returning a value, there's no way to get the first part of it.

Instead, write an assignment tag, which sets a variable in the context. Then you can do this:

{% name foo as bar %}
{{ bar.0 }}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top