Search code examples
pythondjangopython-3.xdjango-templatesdjango-2.0

How to use template context variable's value inside Django template tags?


In Django, I have seen the trans tag, it is useful to place translation strings in templates.

{% trans 'Rishikesh' %}

But I want to use dynamic value in place of 'Rishikesh' that is being passed by my view function.

Let you assume my view function named index is as follows.

def index(request):
    return render(request, "index.html", {"username": "Rishikesh"})

My question is, I want to use the value of username as value of trans tag in my template something like following.

{% trans "{{username}}" %}

I think, it should work but it doesn't work?

Please help me if you know any way to do the same.


Solution

  • Try to use variables without quotes and without braces like this:

    {% trans username %}