像下面这段代码在WEBPY 0.40里会发现出错:
$def with (page_posts, page_count, page)
$code:
grace = 5
range = grace * 2
start = page - grace if page - grace > 0 else 1
end = start + range
if end > page_count:
end = page_count
start = end - range if end - range > 0 else 1
<div id="new_post">
<a href="/add">+发帖</a>
</div>
<table id="post_list">
<tbody>
<tr>
<th class="title">话题</th>
<th class="author">作者</th>
<th class="reply_count">回应</th>
<th class="last_reply_time">最后回应</th>
</tr>
$for p in page_posts:
<tr>
<td title="$p['title']"><a href="/view/$p['id']">$trim_utf8(p['title'], 25)</a></td>
<td><a href="/user/$p['userid']">$p['username']</a></td>
<td>$p['comment_count']</td>
<td>$p['last_time']</td>
</tr>
</tbody>
</table>
<div id="post_pager">
$if start > 1:
<a class="page" href="/?page=1">1</a> ...
$for i in range(start, end+1):
$if i == page:
<span class="page">$i</span>
$else:
<a class="page" href="/?page=$i">$i</a>
$if end < page_count:
... <a class="page" href="/?page=$page_count">$page_count</a>
</div>
运行这段代码会出错如下:
Traceback (most recent call last):
File "D:\AI\python\web\caiforum\forumTest.py", line 41, in <module>
print(titled_render().list(page_posts, page_count, page))
File "C:\Users\tony\AppData\Local\Programs\Python\Python36\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\template.py", line 1037, in template
return self._base(t(*a, **kw))
File "C:\Users\tony\AppData\Local\Programs\Python\Python36\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\template.py", line 904, in __call__
return BaseTemplate.__call__(self, *a, **kw)
File "C:\Users\tony\AppData\Local\Programs\Python\Python36\lib\site-packages\web.py-0.40.dev0-py3.6.egg\web\template.py", line 831, in __call__
return self.t(*a, **kw)
File "D:\AI\python\web\caiforum/templates\list.html", line 40, in __template__
<a class="page" href="/?page=$i">$i</a>
TypeError: 'int' object is not callable
出错的原因是暂时没有找到,只能把这部分代码转换到forum.py文件,修改模板文件list.html,如下:
$def with (page_posts, page_count, page, start, end)
<div id="new_post">
<a href="/add">+发帖</a>
</div>
<table id="post_list">
<tbody>
<tr>
<th class="title">话题</th>
<th class="author">作者</th>
<th class="reply_count">回应</th>
<th class="last_reply_time">最后回应</th>
</tr>
$for p in page_posts:
<tr>
<td title="$p['title']"><a href="/view/$p['id']">$trim_utf8(p['title'], 25)</a></td>
<td><a href="/user/$p['userid']">$p['username']</a></td>
<td>$p['comment_count']</td>
<td>$p['last_time']</td>
</tr>
</tbody>
</table>
<div id="post_pager">
$if start > 1:
<a class="page" href="/?page=1">1</a> ...
$for i in range(start, end+1):
$if i == page:
<span class="page">$i</span>
$else:
<a class="page" href="/?page=$i">$i</a>
$if end < page_count:
... <a class="page" href="/?page=$page_count">$page_count</a>
</div>