初学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 ...
随机推荐
- angularjs中的directive
正在初学angularjs中,在网上看到一篇详细讲解directive指令的文章,于是就记录在这里和大家一起分享 angular.module('docsTransclusionExample', [ ...
- [Bootstrap]组件(四)
导航条 移动设备上折叠(可开关),超过规定视口标签时候水平展开模式 依赖插件 注意点:导航条内元素过多或其他原因导致元素内元素宽度过长,会引起折行-->解决:a.减少导航内元素或者减少宽度 b ...
- Eclipse 快捷键 快捷输入
快捷键: 1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如appli ...
- android 数据库的增删改查的另一种方式
老师笔记 # 3 Android下另外一种增删改查方式 1.创建一个帮助类的对象,调用getReadableDatabase方法,返回一个SqliteDatebase对象 2.使用Sq ...
- (转载)浅谈我对DDD领域驱动设计的理解
原文地址:http://www.cnblogs.com/netfocus/p/5548025.html 从遇到问题开始 当人们要做一个软件系统时,一般总是因为遇到了什么问题,然后希望通过一个软件系统来 ...
- DB2&&oracle-培训内容
DB2 1 Data Sharing架构,高可用 2 DB2的对象主要有以下几类:database,storage group, Tablspace, indexspace, table,index ...
- WCF 服务的ABC之契约(七)
契约 Contract WCF的所有服务都会公开为契约(Contract),契约与平台无关,是描述服务功能的标注方式. 服务契约(Service Contract) 服务契约描述的是一个服务,它定义了 ...
- nginx自启动脚本
#!/bin/bash # #Startup script for Nginx - this script starts and stops the nginx daemon # # chkconfi ...
- openerp学习笔记 自定义小数精度(小数位数)
小数位数标识定义: lx_purchase/data/lx_purchase_data.xml <?xml version="1.0" encoding="utf- ...
- Git客户端Windows下的使用
1,必须安装的软件 msysgit http://code.google.com/p/msysgit/downloads/list?q=full+installer+official+git (ms ...