Django TemplateSyntaxError:无效的过滤器:'truncatechars'

时间:2013-07-24 19:29:18

标签: python django google-app-engine django-templates webapp2

应用引擎中的

Getting TemplateSyntaxError: Invalid filter: 'truncatechars'。我正在使用最新的django库。根据{{​​3}} django最新版本为1.4,truncatechars包含在同一版本中("Third-party Libraries in Python 2.7 on appengine")。

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'

1 个答案:

答案 0 :(得分:0)

你应该直接使用Django模板化的东西(没有webapp包装器)。

How do I use Django templates without the rest of Django?

或使用推荐的Jinja2模板引擎。

Using Jinja2 templates with Google App Engine