从数据库加载django模板

我试图从 djangos正常请求 – 响应结构之外的数据库渲染一个django模板.但是,由于django模板的编译方式,这似乎是不平凡的.我想做这样的事情:
>>> s = Template.objects.get(pk = 123).content
>>> some_method_to_render(s,{'a' : 123,'b' : 456})
>>> ... the rendered output here ...

你怎么做到这一点?

解决方法

这没有什么复杂的,它与请求/响应结构没有任何关系.所有您需要做的是将模板字符串传递到django.template.Template构造函数(BTW,我已经更改了模型的名称,以避免混淆):
from django.template import Context,Template
from myapp.models import DbTemplate

s = DbTemplate.objects.get(pk=123).content
tpl = Template(s)
tpl.render(Context(dict(a=123,b=456)))

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...