[Fixed]-How to use raw python code in a Django template?

18👍

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/

3👍

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

2👍

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…).

2👍

You can use verbatim tag in Django 1.5 onwards as follows:

{% verbatim myblock %}
    Avoid template rendering via the {% verbatim %}{% endverbatim %} block.
{% endverbatim myblock %}

1👍

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.

0👍

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.

Leave a comment