How to use raw python code in a Django template?


Question

In smarty templates you can use raw PHP code in a template by placing it within the "literal" template tag:

{literal}

echo 'hello world';

{/literal}

How can you use raw python code in a Django template?

1
14
5/22/2011 12:53:41 AM

Accepted Answer

You can't, that's why you have to create custom template tag which runs the functionality. Here's more information on how to do that:

http://docs.djangoproject.com/en/dev/howto/custom-template-tags/

16
5/22/2011 12:56:19 AM

You cannot use python code in django template. This is by design, Django's idea of template is to isolate the presentation logic from the programming code.

Django documentation states :

Django’s template engine provides a powerful mini-language for defining the user-facing layer of your application, encouraging a clean separation of application and presentation logic


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon