初学tornado之MVC版helloworld
文接上篇,看我一个简单的helloworld,虽然觉得这个框架着实精小,但是实际开发总不能这么用。所以还是应该按照实际开发来写一个helloworld。
既然是实际项目版的helloworld,那就要有组织结构,不能代码都塞在一个文件里。
大体结构如下:
mvc_helloworld
--__init__.py
--urls.py
--application.py
--server.py
--handlers
----__init__.py
----index.py
--model
----__init__.py
----entity.py
--static
----css
------index.css
----js
----img
--templates
----index.html
这是一个简单的mvc结构,通过urls.py来控制访问,通过handlers来处理所有的访问,通过model来处理持久化的内容。剩下的static和templates就不用说了。另外可以通过在model和handlers之间增加cache层来提升性能。
下面逐一给出实例代码: server.py,用来启动web服务器:
#coding:utf-8 import tornado.ioloop
import sys from application import application PORT = '8080' if __name__ == "__main__":
if len(sys.argv) > 1:
PORT = sys.argv[1]
application.listen(PORT)
print 'Development server is running at http://127.0.0.1:%s/' % PORT
print 'Quit the server with CONTROL-C'
tornado.ioloop.IOLoop.instance().start()
application.py,可以作为settings:
#coding:utf-8
#author:the5fire from urls import urls import tornado.web
import os
SETTINGS = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
) application = tornado.web.Application(
handlers = urls,
**SETTINGS
)
urls.py:
#coding:utf-8 from handlers.index import MainHandler urls = [
(r'/', MainHandler),
]
handlers/index.py:
#coding:utf-8 import tornado.web
from model.entity import Entity class MainHandler(tornado.web.RequestHandler):
def get(self):
entity = Entity.get('the5fire\'s blog')
self.render('index.html', entity = entity)
model/entity.py:
#coding:utf-8 class Entity(object):
def __init__(self, name):
self.name = name @staticmethod
def get(name):
return Entity(name)
templates/index.html:
<!DOCYTYPE html>
<html>
<head>
<meta type="utf-8">
<title>首页</title>
<link href="/static/css/index.css" media="screen" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>Hello, tornado World!</h1>
<h2>by <a href="http://www.the5fire.com" target="_blank">{{entity.name}}</a></h2>
</body>
</html>
static/css/index.css:
/**
author:the5fire
**/ body {
background-color:#ccc;
}
大体上就这些,当然所有的东西都不是不可变的,应该按照自己的喜好来写。 最后运行的时候通过:python server.py 8000 代码可以在线查看,我的github库,有很多代码哦:https://github.com/the5fire/practice_demo/tree/master/learn_tornado/mvc_hello
初学tornado之MVC版helloworld的更多相关文章
- .Net Core3.1 + EF Core + LayUI 封装的MVC版后台管理系统
项目名称:学生信息管理系统1.0 后台框架:.Net Core 3.1 + EF Core yrjw.ORM.Chimp 前端框架:ASP.NET Core MVC + LayUI + Bo ...
- RDIFramework.NET-.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(MVC版)
RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(MVC版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之产品管理 ...
- 红包项目总结---MVC版
起因: 针对传统版的明显缺陷做优化.主要是提升可维护性. 效果 线上: 未发布 线下:http://10.27.5.1/svn/FED/code/hongbao/year-end hb-fac ...
- Java飞机大战MVC版
PlaneWar Java飞机大战MVC版 //无聊时偷的雷霆战机素材写了一个飞机大战,本意是练习mvc,但写得还是不清晰 github下载:https://github.com/dejavudwh/ ...
- Delphi_01_控制台版HelloWorld
对于Windows下的控制台编程,我相信很多人都不陌生.而C语言开始的著名的“Hello world”程序基本是学习编程的第一步.我想对于 RAD开发,大家熟悉的一般都是GUI编程,而对于consol ...
- 初学C#和MVC的一些心得,弯路,总结,还有教训(2)--关于Entity Framework
看了一堆视频教程后,感觉基本了解的差不多了,可以动手.....因为最好的学习方法就是实践嘛.... 所以打算从网站做起,在WebForm和MVC之间选了MVC,因为感觉高大上...也比较灵活 于是买了 ...
- Qt版helloworld
跟学别的编程语言一样,Qt也不例外,一开始就想写一个helloworld.初学Qt十几天,看了一点关于Qt视频的介绍和书上的基础知识,对于Qt写工程的概念有了初步的认识,就代码的形式来说,Qt Cre ...
- java之spring mvc之helloworld
这篇主要讲解springmvc的基本的使用,这里以helloworld项目为例. 目录结构: 1. 新建 web 项目 :springmvc_helloworld 2. 在 WebRoot\WEB-I ...
- 动态生成验证码———MVC版
上面有篇博客也是写的验证码,但那个是适用于asp.net网站的. 今天想在MVC中实现验证码功能,弄了好久,最后还是看博友文章解决的,感谢那位博友. 首先引入生成验证码帮助类. ValidateCod ...
随机推荐
- C#参数化SQL查询
//写一个存储过程 ALTER PROCEDURE dbo.Infosearch ( @bmid smallint = null, @xm varchar()=null, @xb varchar()= ...
- HTML5+CSS3+JQuery打造自定义视频播放器
来源:http://www.html5china.com/HTML5features/video/201109206_1994.html 简介HTML5的<video>标签已经被目前大多数 ...
- Flex-box 学习
.flex-cont{ /*定义为flexbox的“父元素”*/ display: -webkit-box; display: -webkit-flex; display: flex; /*子元素沿主 ...
- PHP利用socket_bind函数切换IP地址采集数据
在利用PHP进行数据采集的过程中,通常会遇到IP被屏蔽或出现验证码的情况:为了能够继续采集,我们需要切换不同的ip,每访问一次,随机切换一个IP.当然也可以通过收集大量代理,通过切换代理的方式进行采集 ...
- 错误解决mysql - Event Scheduler: No data - zero rows fetched, selected, or processed
当遇到一个NOT FOUND(无数据)的警告时,使用一个包含清除警告语句的条件句柄处理,就可以继续处理程序并退出句柄. 这个问题在MySQL5.6.3之后的版本已经解决了,所以该解决方法不是必要的. ...
- iblog语法高亮示例
-------------------------------------------------------------------------------------- iblog 是一款 Sub ...
- 在有跳板机的情况下,SecureCRT自动连接到目标服务器
为了服务器的安全,运维人员经常会要求我们先登录到跳板机,然后再SSH连接到目标服务器.但是这样是很繁琐的,每次在SecureCRT创建一个连接,都需要输入SSH命令,然后输入密码. 下面的方法可以实现 ...
- (转)使用getevent监听Android输入设备文件
尊重原创转载请注明:From AigeStudio(http://blog.csdn.net/aigestudio)Power by Aige 侵权必究! 炮兵镇楼 上一节Android事件分发完全解 ...
- Basic Vlan Concepts
1. Vlan Benefit ·To reduce CPU overhead on each device by reducing the number of devices that recei ...
- Codeforces 559A 第六周 O题
Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...