Tornado is a non-blocking web server and an excellent python web framework. There are a lot of and details about django tutorial on the Internet, and tornado is rarely used, I want to build a small website with this excellent tornado framework step by step in my way from scratch. On the one hand, I want to deepen my understanding, on the other hand, I want to accept suggestions and criticism from everyone, and make more progress ~ I registered a Domain Name: http://www.ustchacker.com, because the server in the school, the Internet can not access, wait until the function is complete and then mounted to the Internet.
I want to build a common website with login, registration, post, comment, group chat, and other functions that can be improved later. The Code is as follows:
import osimport tornado.webimport tornado.ioloopfrom handlers import *handlers=[(r'/',indexHandler),(r'/member',memberHandler),(r'/chat/(\d+)',chatHandler),(r'/register',registerHandler),(r'/logout',logoutHandler),(r'/post',postHandler),(r'/user/(\w+)',userHandler),(r'/blog/(\d+)',blogHandler),(r'/comment',commentHandler),]settings={'static_path':os.path.join(os.path.dirname(__file__),'static'),'template_path':os.path.join(os.path.dirname(__file__),'template'),}app=tornado.web.Application(handlers,**settings)app.listen(8888)tornado.ioloop.IOLoop.instance().start()
/Corresponding to the homepage;/member corresponding to the registered member list;/chat/(\ d +) corresponding to the group chat page, \ d + indicates the number of pages, the reason is that it is better to add more pages to the group chat topic;/register corresponds to registration;/logout corresponds to logout;/post corresponds to post, And/user/(\ w +) corresponds to the homepage of each registered user; /blog/(\ d +) corresponds to each article;/comment is used to post comments.
Create html files in the template directory:
blog.html index.html member.html register.htmlchat.html main.html post.html user.html
A database is also needed. mongodb is very easy to use. However, for the sake of convenience and simplicity, it is okay for the small site to use sqlite3 ~ There are a lot of ORM for databases. Currently, the small site features are simple and I just want to encapsulate it myself.
The frontend frame uses bootstrap, which is in the main model main.html as follows:
<meta charset='utf-8'>
To add bootstrap.css and bootstrap. js, because bootstrap. js depends on jquery, The jquery-2.1.1.js also needs to be added. Later, we will continue to improve and add the small-site function through the following articles, and show the design beauty of tornado in the process.
Reprinted Please note: