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

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

问题描述

我有几个小的(500或600行模板代码)Django网站,我想将它们迁移到使用Jinja2 ...但我想了解一下这将是多少工作。那么,一般来说,将一个小的Django站点迁移到Jinja2有多少工作?这是值得吗?

解决方案

虽然这只是我自己的经验,我发现从Django转换为Jinja2是值得的由于以下原因:


  1. Jinja2的设计和实现对我来说似乎对于软件开发人员和模板设计师来说更为直观; li>
  2. Jinja2是更可扩展的(至少在我试图扩展我的模板引擎的方式);

  3. Jinja2在允许逻辑方面更加灵活代码被运行(但它给你足够的绳子来挂自己);

  4. Jinja2被认为是更快(虽然我没有做任何基准,这总是受到辩论取决于所使用的测试,并且在任何情况下,对于必须进行数据库查找的查询的总等待时间无关紧要);

  5. Jinja2提供了更多有用的错误输出比Django(即跟踪到行号在模板中的错误oc编辑:根据Dor的评论,Django提供了一些有用的错误信息,指出了问题的界限和上下文,就像Jinja2一样。

如果Django的模板引擎没有任何问题,Jinja2应该会觉得比较直观,如果还有一点更加清晰(或者对我来说无论如何) 。同样,我发现 Coffin 项目写得很好从Django转换到Jinja2–无论是为了使用,还是如何扩展Jinja2的一个例子。



所有这一切,Django的模板引擎是坚实的,相当有能力进行大多数任务。我相信在Django的下一个版本中它正在得到改进,相当多的专门的开发人员来说,还有相当多的努力增加它的功能。因此,在近期至中期的未来,不用担心它不受支持。



再次,这只是我的经验,为什么值得–我希望这很有帮助。


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"?

解决方案

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

  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.

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.

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天全站免登陆