2013-07-24 93 views
0

Getting TemplateSyntaxError: Invalid filter: 'truncatechars'在應用程序引擎中。我正在使用最新的django庫。根據"Third-party Libraries in Python 2.7 on appengine" django最新版本是1.4和truncatechars包含在同一版本(django doc)。Django TemplateSyntaxError:無效的過濾器:'truncatechars'

index.py

import webapp2 
import os 
from google.appengine.ext.webapp import template  

class MainHandler(webapp2.RequestHandler): 
    def get(self): 
     title = " Python, an interactive, object-oriented, extensible programming language." 
     template_values = { 
      'title': title, 
     } 
     path = os.path.join(os.path.dirname(__file__), 'index.html') 
     self.response.out.write(template.render(path, template_values)) 

的index.html

<html> 
<body> 
{{title|truncatechars:9}} // Error returns 

{{title|slice:"9"}}{% if title|length > 9 %}...{% endif %} 
</body> 
</html> 

app.yaml中

libraries: 
    - name: django 
     version: latest 

我試圖與庫version: "1.4"和有同樣的問題。

錯誤:

File "/home/user/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "/home/user/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1595, in handle_exception 
    return handler(request, response, e) 
    File "/home/user/dimensionfinder/urls.py", line 43, in handle_500 
    response.write(template.render(path, template_values)) 
    File "/home/user/google_appengine/google/appengine/ext/webapp/template.py", line 89, in render 
    t = _load_internal_django(template_path, debug) 
    File "/home/user/google_appengine/google/appengine/ext/webapp/template.py", line 163, in _load_internal_django 
    template = django.template.loader.get_template(file_name) 
    File "/home/user/google_appengine/google/appengine/_internal/django/template/loader.py", line 160, in get_template 
    template = get_template_from_string(template, origin, template_name) 
    File "/home/user/google_appengine/google/appengine/_internal/django/template/loader.py", line 168, in get_template_from_string 
    return Template(source, origin, name) 
    File "/home/user/google_appengine/google/appengine/_internal/django/template/__init__.py", line 158, in __init__ 
    self.nodelist = compile_string(template_string, origin) 
    File "/home/user/google_appengine/google/appengine/_internal/django/template/__init__.py", line 186, in compile_string 
    return parser.parse() 
    File "/home/user/google_appengine/google/appengine/_internal/django/template/__init__.py", line 281, in parse 
    compiled_result = compile_func(self, token) 
    File "/home/user/google_appengine/google/appengine/_internal/django/template/defaulttags.py", line 693, in do_for 
    nodelist_loop = parser.parse(('empty', 'endfor',)) 
    File "/home/user/google_appengine/google/appengine/_internal/django/template/__init__.py", line 262, in parse 
    filter_expression = self.compile_filter(token.contents) 
    File "/home/user/google_appengine/google/appengine/_internal/django/template/__init__.py", line 356, in compile_filter 
    return FilterExpression(token, self) 
    File "/home/user/google_appengine/google/appengine/_internal/django/template/__init__.py", line 538, in __init__ 
    filter_func = parser.find_filter(filter_name) 
    File "/home/user/google_appengine/google/appengine/_internal/django/template/__init__.py", line 362, in find_filter 
    raise TemplateSyntaxError("Invalid filter: '%s'" % filter_name) 
TemplateSyntaxError: Invalid filter: 'truncatechars' 
+0

'version:「1.4」'(雙引號)? –

+0

如果它不起作用,您可以始終使用'| slice:「:9」' –

+1

您應該顯示您用於呈現模板的代碼。你實際上並沒有使用Django,你正在使用webapp,所以你必須做一些事情來導入它並使用模板渲染組件,並且問題必須在那裏。 –

回答