Ser*_*rov 13 html python jinja2
说我用return render_template('index.html', users=users).是否可以在模板中获取文件名而无需在视图中显式发送?
pat*_*atb 13
虽然没有文档,但{{ self._TemplateReference__context.name }}会给出模板名称.并且您可以使用许多有趣的属性self._TemplateReference__context.
例如,您可以将其添加到最顶层的基本模板中:
<meta name="template" content="{{ self._TemplateReference__context.name }}">
Run Code Online (Sandbox Code Playgroud)
因此,查看页面源可以帮助您快速找到相关的模板文件.如果您不想公开此类信息,请将其作为测试环境的条件.
如果你需要的只是basename,你可以使用{{ self }}它返回一个包含模板基名的repr字符串,例如<TemplateReference 'view.html'>.您可以使用过滤器对其进行解析,例如,{{ self | quoted }}
@app.template_filter('quoted')
def quoted(s):
l = re.findall('\'([^\']*)\'', str(s))
if l:
return l[0]
return None
Run Code Online (Sandbox Code Playgroud)
如果您需要完整路径,例如'/full/path/to/view.html',您可能想要继承模板.
| 归档时间: |
|
| 查看次数: |
6121 次 |
| 最近记录: |