2011-05-23 41 views
2

我想在web應用程序之外使用Django模板庫。我可以傳遞上下文變量來構造一個html文檔。我有加載模板並構造html數據的python代碼。當我嘗試運行該腳本時,出現以下錯誤:如何在Web應用程序之外加載Django模板?

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

有沒有辦法做到這一點?

+0

爲了模板可維護性和HTML有效負載大小,您應該將元素樣式切換爲CSS類。 {%cycle%}模板標籤還可以讓您減少模板的「奇怪」「甚至」部分(http://docs.djangoproject.com/en/1.3/ref/templates/builtins/#cycle) – Manfre 2011-05-24 15:17:59

回答

1

我試圖重現你的錯誤,沒有運氣。這是(http://pastebin.com/k116mbXC)在django應用程序之外呈現模板的簡單腳本(基於您的代碼)。適用於Django 1.3。

相關問題