0

Getting TemplateSyntaxError: Invalid filter: 'truncatechars'在应用引擎中。我正在使用最新的 django 库。根据“appengine 上 Python 2.7 中的第三方库”, django 最新版本是 1.4,并且truncatechars包含在同一版本(django doc)中。

索引.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))

索引.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'
4

1 回答 1

0

您应该直接使用 Django 模板(没有 webapp 包装器)。

如何在没有 Django 其余部分的情况下使用 Django 模板?

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

在 Google App Engine 中使用 Jinja2 模板

于 2013-07-28T19:32:51.843 回答