Looking at the source, while the Template object gains access to the template name (via .name ), this value is also never passed to the Parser object and, therefore, is not available for template tags.
There are various ways to make the template name available to the template itself (by adding it to the context), but not in the template tags.
As Daniel Roseman said in the comments, if you can clarify what you are actually trying to achieve, there may be a better way to achieve what you want. Do not be offended, but it looks like it might be an XY problem.
Of academic interest, I had a quick violin to see if this was possible. As far as I can see, this is possible, but not without a change or a monkey fixing the source of django.
Note. The following is not a recommended solution and just hints at what might be required for the actual work. Do not use for production code.
By changing django.template.base.py with the following changes, we will add the .template_name attribute to the parser object, making it available for template tags.
To verify this, I defined the following tag, which simply returns the name of the template in the header:
from django.template.base import Node, Library register = Library() class TemplateNameNode(Node): def __init__(self, template_name): self.name = template_name def render(self, context): return self.name.upper() @register.tag def caps_template_name(parser, token): return TemplateNameNode(parser.template_name)
and the following template:
{% load mytags %} Template name in caps: {% caps_template_name %}
This seems to work when testing in ./manage.py shell :
>>> from django.template import loader, Context >>> t = loader.get_template("test.html") >>> t.render(Context({})) u'\nTemplate name in caps: TEST.HTML\n'
While this works, I have to reiterate that manually fixing the django source will never be a good solution and is subject to all kinds of misfortunes when switching to different versions.