如何在 Web 应用程序之外加载 Django 模板?
我正在尝试在 Web 应用程序之外使用 Django 模板库。我可以传递上下文变量来构造 html 文档。我有 python 代码加载模板并构造 html 数据。当我尝试运行该脚本时,出现以下错误:
raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
django.template.InvalidTemplateLibrary: Template library django.template.loader_tags does not have a variable named 'register'
如何解决此问题?
我已验证以下内容:我的所有模板均位于指定的 TEMPLATE_DIRS 目录中。我的Python代码如下:
def LoadAndRenderTemplate(self, template_name,
items_to_be_rendered, blank_row=''):
"""Loads and renders the django template.
Args:
template_name: template path relative to TEMPLATE_DIRS
items_to_be_rendered: items to be rendered
blank_row: string flag value having permissible values 'odd' and 'even'
responsible for rendering BLANK_ODD_ROW and BLANK_EVEN_ROW
if the flag value is 'odd' and 'even' respectively.
Returns:
safe representation of html output
"""
loaded_template = django_dep.loader.get_template(template_name)
context = django_dep.template.Context({'report_date': self.report_date,
'blank_row': blank_row,
'items': items_to_be_rendered})
return loaded_template.render(context)
基本模板(base.html)
{% block body %}{% endblock %}
{% block odd_even_row %}
{% ifequal blank_row "odd" %}
<!-- BLANK ODD ROW -->
<tr style=width:100%;font-weight:bold;background-color:#FFFFFF;
font-size:11px;>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
{% endifequal %}
{% ifequal blank_row "even" %}
<!-- BLANK EVEN ROW -->
<tr style=width:100%;background-color:#EEEEEE;font-size:11px;>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
{% endifequal %}
{% endblock %}
顶部部分模板
{% extends "base.html" %}
{% block body %}
<html>
<head></head>
<body style=font-family:arial;>
<ul>
<li>Dashboard</li>
</ul><h3 style=color:#3366CC;>Student List as of {{ report_date }}</h3>
<table style=border-collapse:collapse;background-color:#EEEEEE>
<tr style=width:100%;font-weight:bold;background-color:
#E5ECF9;color:#3366CC;font-size:10px;>
<td style=width:4em;>First Name</td>
<td style=width:100em;>Last Name</td>
</tr>
</table>
</body>
</html>
{% endblock %}
有没有办法做到这一点?
I am trying to use Django template library outside of the web app. I can pass contextual variables to construct a html document. I have python code that loads the template and constructs the html data. When I try to run the script, I get the following error:
raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
django.template.InvalidTemplateLibrary: Template library django.template.loader_tags does not have a variable named 'register'
How do I fix this issue?
I have verified the following that all my templates are in my specified TEMPLATE_DIRS
directory. My Python code is given below:
def LoadAndRenderTemplate(self, template_name,
items_to_be_rendered, blank_row=''):
"""Loads and renders the django template.
Args:
template_name: template path relative to TEMPLATE_DIRS
items_to_be_rendered: items to be rendered
blank_row: string flag value having permissible values 'odd' and 'even'
responsible for rendering BLANK_ODD_ROW and BLANK_EVEN_ROW
if the flag value is 'odd' and 'even' respectively.
Returns:
safe representation of html output
"""
loaded_template = django_dep.loader.get_template(template_name)
context = django_dep.template.Context({'report_date': self.report_date,
'blank_row': blank_row,
'items': items_to_be_rendered})
return loaded_template.render(context)
Base Template (base.html)
{% block body %}{% endblock %}
{% block odd_even_row %}
{% ifequal blank_row "odd" %}
<!-- BLANK ODD ROW -->
<tr style=width:100%;font-weight:bold;background-color:#FFFFFF;
font-size:11px;>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
{% endifequal %}
{% ifequal blank_row "even" %}
<!-- BLANK EVEN ROW -->
<tr style=width:100%;background-color:#EEEEEE;font-size:11px;>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
{% endifequal %}
{% endblock %}
Top Section Template
{% extends "base.html" %}
{% block body %}
<html>
<head></head>
<body style=font-family:arial;>
<ul>
<li>Dashboard</li>
</ul><h3 style=color:#3366CC;>Student List as of {{ report_date }}</h3>
<table style=border-collapse:collapse;background-color:#EEEEEE>
<tr style=width:100%;font-weight:bold;background-color:
#E5ECF9;color:#3366CC;font-size:10px;>
<td style=width:4em;>First Name</td>
<td style=width:100em;>Last Name</td>
</tr>
</table>
</body>
</html>
{% endblock %}
Is there a way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试重现您的错误,但没有成功。这是(http://pastebin.com/k116mbXC)简单的脚本(基于您的代码),它在 django 应用程序之外呈现模板。适用于 Django 1.3。
I've tried to reproduce your error, with no luck. Here's (http://pastebin.com/k116mbXC) the simple script (based on your code) that renders template outside of django application. Works on Django 1.3.