python web py入门(6)-webpy在模板里使用code代码段错误的问题

像下面这段代码在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>

比特币源码入门教程

https://edu.csdn.net/course/detail/6998

深入浅出Matplotlib
https://edu.csdn.net/course/detail/6859

深入浅出Numpy
http://edu.csdn.net/course/detail/6149 

Python游戏开发入门

你也能动手修改C编译器




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值