Skip to main content
 首页 » 编程设计

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

2025年05月04日8shangdawei

我正在尝试在 Web 应用程序之外使用 Django 模板库。我可以传递上下文变量来构建 html 文档。我有加载模板和构建 html 数据的 python 代码。当我尝试运行脚本时,出现以下错误:

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 %} 

有办法吗?

请您参考如下方法:

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