Search code examples
djangotuplesdjango-custom-tags

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


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?


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