首页 模板页面 index.html
1. 修改TD类GET方法
class TD(object): #针对待办事件列表 处理类
def GET(self):
'''
查看待办事件
:return:
'''
i = web.input()
print i
page = int(i.get('page', 1))
limit = int(i.get('limit', 10))
skip_num = (page-1)*10
results = []
for post_data in collection.find().skip(skip_num).limit(limit):
format_data = formatField(post_data)
results.append(format_data)
#web.header('Content-Type', 'application/json')
#return json.dumps(results)
return render.index(results)
2. 定义全局render
web.template.Template.globals['render'] = render
3. templates下编辑 index.html
By default, Templetor uses web.websafe filter to do HTML-encoding.
>>> render.hello("1 < 2")
"Hello 1 < 2"
To turnoff filter use : after $. For example:
The following will not be html escaped.
$:form.render()
代码内容如下:
$def with(todos)
$:render.header()
<div class="box">
<div class="box todos">
<h2 class="box">待办事项</h2>
<ul>
$for todo in todos:
$ status = ''
$if 'finished' in todo:
$if todo['finished']==True:
$ status = 'class="finished"'
<li $:status>
$# 未完成的任务
$if not status:
$todo['title']
<a href ="javascript:void(0)" onclick="finish('$todo['_id']','yes');">完成</a>,
$else:
$# 已经完成的任务
<del>$todo['title']</del>
<a href ="javascript:void(0)" onclick="finish('$todo['_id']','no');">恢复</a>,
<a href ="/TodoLists/$todo['_id']" onclick="update('$todo['_id']');">修改</a>,
<a href ="javascript:void(0)" onclick="deleteTD('$todo['_id']');">删除</a>
</li>
</ul>
</div>
<div class="box post">
<h2>新增</h2>
<p><input type="text" id="title" class="long_txt" /></p>
<p><input type="button" value="添加" onclick="createTD()" /></p>
</div>
</div>
<script language=javascript>
function finish(obj,data) {
alert(data);
$$.ajax({
type: "PATCH",
url:"/TodoLists/"+obj,
data:{
'finished':data
},
success:function(result){
alert("数据: \n" + result + "\n" );
window.location.href ='/TodoLists';
},
error:function() {
//alert(arguments[1]);
alert("异常!");
}
});
}
function deleteTD(obj){
alert(obj);
$$.ajax({
type: "DELETE",
url:"/TodoLists/"+obj,
data:{
},
success:function(result){
alert("数据: \n" + result + "\n" );
window.location.href ='/TodoLists';
},
error:function() {
//alert(arguments[1]);
alert("异常!");
}
});
};
function createTD(){
alert(document.getElementById("title").value);
$$.ajax({
type: "POST",
url:"/TodoLists",
data:{
'title':document.getElementById("title").value
},
success:function(result){
alert("数据: \n" + result + "\n" );
window.location.href ='/TodoLists';
},
error:function() {
//alert(arguments[1]);
alert("异常!");
}
});
};
</script>
$:render.foot()