Reputation: 175
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?
Upvotes: 16
Views: 34389
Reputation: 61
You simply can't! .You just can access some python raw code inside the jinja templates like calling a dictionary variable using a for loop etc and using custom templates you can extend the template engine by defining custom tags and filters using Python, and then make them available to your templates using the {% load %} tag.
Upvotes: 0
Reputation: 1178
You can use verbatim
tag in Django 1.5 onwards as follows:
{% verbatim myblock %}
Avoid template rendering via the {% verbatim %}{% endverbatim %} block.
{% endverbatim myblock %}
Upvotes: 2
Reputation: 76
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’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
Upvotes: 3
Reputation: 6536
You cannot. However, there are things like Mako that let you do that, but, mostly its a good idea to keep your logic (i.e. python) away from templates.
In fact, some templates only allow variable substitution.
Upvotes: 1
Reputation: 10268
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/
Upvotes: 18
Reputation: 799150
You can't. Like most sane template engines, Django's doesn't allow that (not that I'm saying that Django is sane or anything...).
Upvotes: 2