Can we use python related api's on the Django templates ?

18 views
Skip to first unread message

ratnadeep ray

unread,
May 27, 2020, 8:21:24 AM5/27/20
to Django users
Hi all, 

Currently I am trying to print the type of a variable from the Django template html file as follows: 

    <center><h2>The type of feature list report for version {%type(version)%} is<h2></center>

For the above, I am getting the following error: 
Invalid block tag on line 9: 'type(version)'. Did you forget to register or load this tag?

So what's going wrong here? How can we use the python related api's (like type) from the html template files? I think inside the {% .... %} we can use python related evaluations. Am I right? 

Please throw some lights on this .

Thanks. 

Daniel Roseman

unread,
May 27, 2020, 9:57:34 AM5/27/20
to Django users
No, you are wrong, and this is explicitly pointed out in the docs (https://docs.djangoproject.com/en/3.0/ref/templates/language/):

> bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic.

--
DR. 

Derek

unread,
May 28, 2020, 2:27:51 AM5/28/20
to Django users
While you cannot use Python operators and functions directly in the templates, you can write your own "wrappers" around those you need:

Reply all
Reply to author
Forward
0 new messages