Django:切换到 Jinja2? [英] Django: Switching to Jinja2?

查看:23
本文介绍了Django:切换到 Jinja2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个小型(500 或 600 行模板代码)Django 站点,我想将它们迁移到使用 Jinja2 中……但我想了解一下它的工作量.那么,一般来说,将一个小型 Django 站点迁移到 Jinja2 大约需要多少工作?它值得"吗?

I've got a couple small (500 or 600 lines of template code) Django sites, and I'd like to migrate them to using Jinja2… But I'd like to get some idea of how much work it will be. So, in general, about how much work is it to migrate a small Django site to Jinja2? And is it "worth it"?

推荐答案

虽然这只是我自己的经验,但我发现从 Django 转换到 Jinja2 是值得的,原因如下:

While it's just my own experience, I found converting from Django to Jinja2 to be worthwhile for the following reasons:

  1. Jinja2 的设计和实现对我来说似乎更直观,无论是作为软件开发人员还是模板设计师;
  2. Jinja2 的可扩展性更强(至少在我试图扩展模板引擎的方式上);
  3. Jinja2 在允许运行逻辑代码方面更加灵活(但它为您提供了足够的绳索来吊死自己);
  4. Jinja2 被认为要快得多(虽然我没有做过任何基准测试,但根据所使用的测试,这总是有争议的,并且无论如何与必须执行 DB 的查询的总等待时间基本无关查找);
  5. Jinja2 提供比 Django 更有用的错误输出(即跟踪到模板中发生错误的行号). 编辑:根据 Dor 的评论,Django提供指向问题行和上下文的有用错误消息,很像 Jinja2.
  1. The design and implementation of Jinja2 seemed more intuitive to me, both as a software developer and template designer;
  2. Jinja2 is more extensible (at least in the ways I've sought to extend my template engine);
  3. Jinja2 is more flexible in terms of permitting logic-code to be run (but it gives you enough rope to hang yourself with);
  4. Jinja2 is regarded as significantly faster (though I haven't done any benchmarks, this is always subject to debate depending on the tests used, and in any event largely irrelevant in the total wait time for a query that has to do DB lookups);
  5. Jinja2 gives significantly more helpful error output than Django (i.e. traces to the line number in the template where the error occurred). Edit: According to Dor's comment, Django gives helpful error messages that point to the line and context of a problem, much like Jinja2.

如果您在使用 Django 的模板引擎时没有遇到任何问题,那么 Jinja2 的应该会感觉相对直观,也许更精致一些(或者至少对我来说是这样).同样,我发现 Coffin 项目写得很好,并且在从Django 到 Jinja2用于它的使用,以及如何扩展 Jinja2 的示例.

If you haven't had any trouble with Django's template engine, Jinja2's should feel relatively intuitive, if perhaps a bit more polished (or it did to me, at any rate). As well, I found the Coffin project well written and reasonably helpful when converting from Django to Jinja2 – both for its use, and as an example of how to extend Jinja2.

尽管如此,Django 的模板引擎非常可靠,并且能够胜任大多数任务.我相信它会在 Django 的下一个版本中得到改进,并且很多专门的开发人员付出了很多努力来增加它的功能.因此,无需担心它在近期和中期的未来会失去支持.

All that being said, Django's template engine is solid and quite capable for most tasks. I believe it's being improved in the next revision of Django, and there is quite a lot of effort to add to its capabilities by quite a number of dedicated developers. As a result there are no worries of it becoming unsupported in the near to medium-term future.

再说一次,这只是我的经验,值得一试–我希望这会有所帮助.

Again, that's just my experience, for what it's worth – I hope that's helpful.

这篇关于Django:切换到 Jinja2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆