I defined one custom tag {% get_user user_id %} which catch user’s id and return user object through variable user_obj: {{ user_obj.email }}, {{ user_obj.name }}, {{ user_obj.photo.url }} and so on.
And it works fine. Next I defined tag {% get_user_items user %} which get user object and returns user’s photo url through variable user_photo: {{ user_photo }}.
I want to make interaction between this 2 custom tags. It means smth like that:
{% load user_tags %}
{% get_user 1 %}
{{ user_obj }} // returns unicode string as expected
{% get_user_items user_obj %}
{{ user_photo }}
As I understand tag ‘get_user_items’ must get a user object through variable user_obj, but no – it gets from variable user_obj the usual string ‘user_obj’, not object!!!
How to fix this? What am I doing wrong??? How can I transmit value from variable {{ user_obj }} to tag {% get_user_items %} directly?
Thanks for your answers in advance!!!
You have to resolve the string as a variable using the context. See discussion on passing template variables to tags in the Django docs
This is the example from the docs. You must first mark the value as a variable using
template.Variable. The argument for that is the argument that holds the variable name. In the example, that’sdate_to_be_formatted. This is saved as an instance variable on the node so it’s available later in therendermethod.Then, in the render method, you attempt to resolve the Variable from the context (
renderhas access to the context, which is why it’s done here). This is done by callingresolveon the instance variable you created in the__init__method and passing it the context as it’s argument. The variable could potentially fail to resolve, which is why it needs to be in atry...exceptblock.