传递到jinja模板时,整数转换为字符串

时间:2016-08-09 17:50:24

标签: python google-app-engine jinja2

我正在尝试在GAE中创建一个新闻聚合器应用程序,因为我无法理解cursor()上的文档,因此我试图实现一种基本的分页形式。

class Display(webapp2.RequestHandler):
    def get(self, page_no):
        is_this_last = False
        list_to_be_displayed_here = feedlist[(int(page_no)-1)*5:int(page_no)*5]
        try:
            is_last = feedlist[page_no*5]
        except:
            is_this_last = True
        self.response.write(render_str('/display.html', page_no=page_no, feedlist=list_to_be_displayed_here, is_this_last=is_this_last))

feedlist是一个全球列表,其中包含我从Google新闻中捕获的RSS Feed。我正在尝试获取10个Feed并在页面中显示5个。 page_no是一个整数(调用上述函数的函数将其硬编码为整数)。

这是display.html

<html>
<body>
    {% for feed in feedlist %}
        <p>{{ feed.title }}</p>
    {% endfor %}
    {% set page_no = page_no + 1 %}
    {% if not is_this_last %}
        <a href="/page_no/display">Next</a>
    {% endif %}
</body>
</html>

由于page_no是一个整数,因此增量应该没有问题。但是我收到一条错误消息,指出无法添加strint个对象。

编辑:我在page_no上使用了int()过滤器,如下所示:

{% set page_no = int(page_no) + 1 %}

这是错误的完整回溯:

Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher
INFO     2016-08-09 23:26:39,196 module.py:787] default: "GET /1/display HTTP/1.1" 500 -
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "C:\Users\IBM_ADMIN\Downloads\7c\NewsAggregatorGAE\news.py", line 93, in get
self.response.write(render_str('/display.html', page_no=page_no, feedlist=list_to_be_displayed_here, is_this_last=is_this_last))
File "C:\Users\IBM_ADMIN\Downloads\7c\NewsAggregatorGAE\news.py", line 38, in render_str
return t.render(params)
File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2-2.6\jinja2\environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "C:\Users\IBM_ADMIN\Downloads\7c\NewsAggregatorGAE\templates\display.html", line 6, in top-level template code
{% set page_no = int(page_no) + 1 %}
UndefinedError: 'int' is undefined

0 个答案:

没有答案