如何在Web应用程序之外加载Django模板?

时间:2011-05-23 03:08:48

标签: python django django-templates

我正在尝试在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&#37;;font-weight:bold;background-color:#FFFFFF;
          font-size:11px;>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
         </tr>
      {% endifequal %}
       {% ifequal blank_row "even" %}
        <!--  BLANK EVEN ROW -->
        <tr style=width:100&#37;;background-color:#EEEEEE;font-size:11px;>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</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&#37;;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 %}

有办法吗?

1 个答案:

答案 0 :(得分:1)

我试图重现你的错误,没有运气。这是(http://pastebin.com/k116mbXC)在django应用程序之外呈现模板的简单脚本(基于您的代码)。适用于Django 1.3。