开发者

How to use raw python code in a Django template?

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?


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/


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


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


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

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜