loader.get_template_from_string()和创建模板有什么区别? [英] What is the difference between loader.get_template_from_string() and creating a Template?

查看:117
本文介绍了loader.get_template_from_string()和创建模板有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力为用户存储自己的模板,我知道我将不得不快速创建模板。例如:

 >>>来自django.template import Template 
>>> Template(Testing {{test}})
< django.template.base.Template对象在0x10c58f990>

但是,当我询问加载模板字符串时,建议您使用 get_template_from_string

 >>> from django.template import loader 
>>>> loader.get_template_from_string(Testing {{test}})
< django.template.base.Template对象在0x10c8b5450>

两种方法有什么区别?是一种更多的pythonic或者是其他的?

解决方案

模板代码的Django文档描述了使用您描述的第一种方法:使用 Template()



它没有提到 get_template_from_string ,也不在文档中的其他任何地方。



因此,我将非常依赖于第一种方法的使用,因为另一种方法可能会在将来更有可能发生变化,因为它是一种未记录的功能。

I've been working on a means for users to store their own templates and I know that I'm going to have to create Templates on the fly. For example:

>>> from django.template import Template
>>> Template("Testing {{test}}")
<django.template.base.Template object at 0x10c58f990>

However, when I asked about loading template strings, I was advised to use get_template_from_string:

>>> from django.template import loader
>>> loader.get_template_from_string("Testing {{test}}")
<django.template.base.Template object at 0x10c8b5450>

What is the difference between the two methods? Is one way more pythonic, or preferred to the other?

解决方案

The Django documentation for the templating code describes using the first method you've described: using Template().

It does not mention get_template_from_string, nor does anywhere else in the documentation.

I would therefore lean heavily on usage of the first method, because the other is presumably more likely to change in future, as it is an undocumented function.

这篇关于loader.get_template_from_string()和创建模板有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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