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

发布于 2024-11-09 10:05:53 字数 3842 浏览 0 评论 0原文

我正在尝试在 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 %}

有没有办法做到这一点?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏半生如梦愿梦如真 2024-11-16 10:05:53

我尝试重现您的错误,但没有成功。这是(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文