Skip to main content
 首页 » 编程设计

python之在没有文件引用的情况下使用 template.render()

2025年05月04日8jpfss

是否可以在没有文件路径的情况下使用 template.render() ?

我想对我的数据存储中的文本属性进行动态字符串替换。立即想到了模板化。也许还有另一种方法?我的想法如下...

my_string = template.render(my_model.description,template_dict) 

请您参考如下方法:

从“google-app-engine”标签来看,我假设您是在谈论 google.appengine.ext.webapp 提供的模板引擎?根据documentation :“为方便起见,webapp 模块包含 Django 的模板引擎”。所以看看Django docs for templates ...

据我所知,您应该能够执行如下操作(我假设 my_model.description 包含您的模板字符串?):

t = template.Template(my_model.description) 
my_string = t.render(template.Context(template_dict)) 

(查看 template.py 的 webapp 代码可能也很有用)