Nic*_*cky 2 python templates bottle
我一直在使用bottlepy,我有这样的事情:
..code..
comments = [(u'34782439', 78438845, 6, u'hello im nick'),
(u'34754554', 7843545, 5, u'hello im john'),
(u'332432434', 785345545, 3, u'hello im phil')]
return comments
Run Code Online (Sandbox Code Playgroud)
在视图中我做了这个:
%for address date user text in comments:
<h3>{{address}}</h3>
<h3>{{date}}</h3>
<h3>{{user}}</h3>
<h3>{{text}}</h3>
%end
Run Code Online (Sandbox Code Playgroud)
当我启动服务器时,错误是:
Error 500: Internal Server Error
Sorry, the requested URL http://localhost:8080/hello caused an error:
Unsupported response type: <type 'tuple'>
Run Code Online (Sandbox Code Playgroud)
我怎么能把它渲染到视图中?
(对不起我的英文)
您的代码有两个问题.首先,响应不能是元组列表.它可以是字符串或字符串列表,正如Peter所建议的那样,或者,如果您想使用该视图,它可以(并且应该)是视图变量的字典.键是变量名(这些名称,例如comments,将在视图中可用),值是任意对象.
因此,您的处理函数可以重写为:
@route('/')
@view('index')
def index():
# code
comments = [
(u'34782439', 78438845, 6, u'hello im nick'),
(u'34754554', 7843545, 5, u'hello im john'),
(u'332432434', 785345545, 3, u'hello im phil')]
return { "comments": comments }
Run Code Online (Sandbox Code Playgroud)
注意@view和@route装饰器.
现在,您的视图代码出现问题:缺少元组解包中的逗号.因此,您的视图(index.html在我的情况下命名)应如下所示:
%for address, date, user, text in comments:
<h3>{{address}}</h3>
<h3>{{date}}</h3>
<h3>{{user}}</h3>
<h3>{{text}}</h3>
%end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1482 次 |
| 最近记录: |