我的应用程序中有一个位于templates/mytemplate.html
的模板,我需要在代码中直接引用该模板文件,将其转换为字符串以与send_mail()
一起使用。我该怎么做?以下是我尝试过的:
from django.template import loader
html_message = loader.render_to_string(
'appname/templates/mytemplate.html',
{
'username': user.name,
}
)
Tags:
如果你的模板就是这样设置的,你应该可以通过名字来引用它
但是,请注意,如果Django项目中存在任何名称冲突(或者即使没有冲突),最好在templates目录中有一个镜像应用程序名称的目录。应用程序的树结构如下所示:
那么您只需将其引用为
loader.render_to_string('appname/mytemplate.html', {...})
相关问题 更多 >
编程相关推荐