初始化tornado目录
构建项目–实现post提交,get展示数据
实现类似
代码逻辑
完整代码
start.py
#!/usr/bin/env python
# coding=utf-8
import time
import tornado.ioloop
import tornado.web
# 业务逻辑处理模块
# 配置选项模块
from controllers import home
# 静态文件和模板文件的配置
settings = {
'template_path': 'templates',
'static_path': 'statics',
}
# 路由模块
## 动态路由系统
application = tornado.web.Application([
(r"/index/(?P<page>\d+)/(?P<nid>\d+)", home.IndexHandler),
],
**settings
)
## wsgi模块
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
controllers/home.py
#!/usr/bin/env python
# coding=utf-8
import tornado.web
LIST_INFO = [
{
"username": "maotai", "email": "123456"},
]
class IndexHandler(tornado.web.RequestHandler):
# def get(self, *args, **kwargs):
def get(self,page,nid):
# print(page,nid)
self.render("home/index.html", list_info=LIST_INFO)
def post(self, *args, **kwargs):
username = self.get_argument("username", "")
email = self.get_argument("email", "")
tmp = {
"username": username, "email": email}
LIST_INFO.append(tmp)
self.redirect("/index/1/1")
home/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>添加数据</h1>
<form action="/index/1/1" method="post">
<input type="text" name="username">
<input type="email" name="email">
<input type="submit" typeof="提交">
</form>
<h1>显示数据</h1>
<table border="1">
<thead>
<tr>
<th>用户名</th>
<th>邮箱</th>
</tr>
</thead>
<tbody>
{% for line in list_info %}
<tr>
<td>{
{ line['username'] }}</td>
<td>{
{ line['email'] }}</td>
</tr>
{% end %}
</tbody>
</table>
</body>
</html>
实现url //
实现分页-每页显示5条数据
效果:
代码逻辑:
完整代码:
#!/usr/bin/env python
# coding=utf-8
import time
import tornado.ioloop
import tornado.web
# 业务逻辑处理模块
# 配置选项模块
from controllers import home
# 静态文件和模板文件的配置
settings = {
'template_path': 'templates',
'static_path': 'statics',
}
# 路由模块
## 动态路由系统
application = tornado.web.Application([
(r"/index/(?P<page>\d*)", home.IndexHandler),
],
**settings
)
## wsgi模块
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
#!/usr/bin/env python
# coding=utf-8
import tornado.web
LIST_INFO = [
{
"username": "maotai", "email": "123456"},
]
class IndexHandler(tornado.web.RequestHandler):
# def get(self, *args, **kwargs):
def get(self, page):
# 每页显示5条数据
# LIST_INFO[0:5]
# LIST_INFO[5:10]
page = int(page)
print(page)
start = (page - 1) * 5
end = page * 5
current_list = LIST_INFO[start:end]
self.render("home/index.html", list_info=current_list)
def post(self, *args, **kwargs):
username = self.get_argument("username", "")
email = self.get_argument("email", "")
tmp = {
"username": username, "email": email}
LIST_INFO.append(tmp)
self.redirect("/index/1")
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>添加数据</h1>
<form action="/index/1" method="post">
<input type="text" name="username">
<input type="email" name="email">
<input type="submit" typeof="提交">
</form>
<h1>显示数据</h1>
<table border="1">
<thead>
<tr>