环境搭建在此不予赘述
1,创建项目所需的文件以及文件夹
1,创建目录:tornado
2,在tornado目录创建static目录用以存储静态文件
3,在tornado目录创建templates目录用以存储模板文件(.html)
4,在tornado目录创建app.py 文件,内容如下:
import os.path
import tornado.auth
import tornado.escape
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import time
from tornado.options import define, options
import pymongo
#导入MongoDB
define("port", default=8001, help="run on the given port", type=int)
#定义监听的端口
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/blog", BlogHandler),
]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True,
)
conn = pymongo.MongoClient(host="localhost", port=27017) # 连接 MongoDB
self.db = conn["blog"]
tornado.web.Application.__init__(self, handlers, **settings)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html",)
def post(self):
title = self.get_argument("title", None)
content = self.get_argument("content", None)
blog = dict()
if title and content:
blog["title"] = title
blog["content"] = content
blog["date"] = int(time.time())
print(blog)
coll = self.application.db.blog
coll.insert(blog)
self.redirect("/blog")
else:
self.redirect("/")
class BlogHandler(tornado.web.RequestHandler):
def get(self):
coll = self.application.db.blog
blog = coll.find_one()
if blog:
self.render("blog.html",
page_title = blog["title"],
blog = blog,
)
else:
self.redirect("/")
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
5,在templates目录创建index.html文件,内容如下:
<html lang="zh-CN">
<head>
<meta charset="utf-8″>
<title>YiRan</title>
<link rel="stylesheet" type="text/css" href="{{ static_url("css/style.css") }}">
<script type="text/javascript">
window.setInterval(function() {
go_to();
},
100);
function go_to() {
if( document.getElementById("myArticle").style.height < (document.getElementById("myArticle").scrollHeight – 4 ) + "px")
document.getElementById("myArticle").style.height = document.getElementById("myArticle").scrollHeight + "px";
}
</script>
</head>
<body>
<div class="main">
<img class="logo" src="{{ static_url("img/logo.png") }}">
<div class="container">
<h1>欢迎访问医然的博客</h1>
<div class="content">
<form method="post">
<p>文章标题:</p>
<input type="text" class="Title" name="title" placeholder="在这里输入你的标题" />
<p>文章正文:</p>
<textarea type="text" class="Article" id="myArticle" name="content" placeholder="在这里输入你的正文"></textarea>
<br/>
<input type="submit" class="Article Button Submit" value="发 布"/>
</form>
</div>
</div>
</div>
</body>
</html>
6,在templates目录创建blog.html文件,内容如下:
<html lang="zh-CN">
<head>
<meta charset="utf-8″>
<title>{{ page_title }}</title>
<link rel="stylesheet" type="text/css" href="{{ static_url("css/style.css") }}">
</head>
<body>
<div class="main">
<img class="logo" src="{{ static_url("img/logo.png") }}">
<div class="container">
<h1>欢迎访问医然的博客</h1>
<div class="content">
<div class="Title">
<p>
{{ blog['title'] }}
<span class="Time">{{ locale.format_date(blog['date'], relative=False) }}</span>
</p>
</div>
<div class="Article">
<p>{{ blog['content'] }}</p>
</div>
</div>
</div>
</div>
</body>
</html>
至此主要文件已经建立完毕,可以使用python执行app.py,然后在浏览器输入:http://localhost:8001 访问刚刚建立的博客啦