作者:the5fire | 标签: MVC  tornado  | 发布:2012-08-06 2:41 p.m.

文接上篇,看我一个简单的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的更多相关文章

  1. .Net Core3.1 + EF Core + LayUI 封装的MVC版后台管理系统

    项目名称:学生信息管理系统1.0 后台框架:.Net Core 3.1 + EF Core    yrjw.ORM.Chimp 前端框架:ASP.NET Core MVC  +  LayUI + Bo ...

  2. RDIFramework.NET-.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(MVC版)

    RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(MVC版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之产品管理 ...

  3. 红包项目总结---MVC版

    起因: 针对传统版的明显缺陷做优化.主要是提升可维护性. 效果  线上:  未发布 线下:http://10.27.5.1/svn/FED/code/hongbao/year-end   hb-fac ...

  4. Java飞机大战MVC版

    PlaneWar Java飞机大战MVC版 //无聊时偷的雷霆战机素材写了一个飞机大战,本意是练习mvc,但写得还是不清晰 github下载:https://github.com/dejavudwh/ ...

  5. Delphi_01_控制台版HelloWorld

    对于Windows下的控制台编程,我相信很多人都不陌生.而C语言开始的著名的“Hello world”程序基本是学习编程的第一步.我想对于 RAD开发,大家熟悉的一般都是GUI编程,而对于consol ...

  6. 初学C#和MVC的一些心得,弯路,总结,还有教训(2)--关于Entity Framework

    看了一堆视频教程后,感觉基本了解的差不多了,可以动手.....因为最好的学习方法就是实践嘛.... 所以打算从网站做起,在WebForm和MVC之间选了MVC,因为感觉高大上...也比较灵活 于是买了 ...

  7. Qt版helloworld

    跟学别的编程语言一样,Qt也不例外,一开始就想写一个helloworld.初学Qt十几天,看了一点关于Qt视频的介绍和书上的基础知识,对于Qt写工程的概念有了初步的认识,就代码的形式来说,Qt Cre ...

  8. java之spring mvc之helloworld

    这篇主要讲解springmvc的基本的使用,这里以helloworld项目为例. 目录结构: 1. 新建 web 项目 :springmvc_helloworld 2. 在 WebRoot\WEB-I ...

  9. 动态生成验证码———MVC版

    上面有篇博客也是写的验证码,但那个是适用于asp.net网站的. 今天想在MVC中实现验证码功能,弄了好久,最后还是看博友文章解决的,感谢那位博友. 首先引入生成验证码帮助类. ValidateCod ...

随机推荐

  1. ThinkPHP控制器

    ThinkPHP控制器Controller 1.什么是控制器 在MVC框架中,其核心就是C(Controller)控制器.主要用于接收用户请求,处理业务逻辑. 2.控制器的定义 在一个ThinkPHP ...

  2. C语言 猜数游戏--产生一个随机数

    #include <stdio.h> #include <time.h> #include <stdlib.h> int main(int argc, const ...

  3. 自定义可判断选项是否正确listbox

    截图如下:        1.实现Converter  获取到listbox,并得到listitem在listbox中的index public class ItemContainerToZIndex ...

  4. devpress控件属性说明表

    XtraEditors 库中所有控件的公共功能 全部都可以绑定数据: 全部都可以独立使用或用于由 Developer Express 提供的容器控件(XtraGrid.XtraVerticalGrid ...

  5. svn迁移

    一.       VisualSVN服务器迁移 场景:把A服务器备份到B服务器 方法: 1.拷贝A上Repositories文件夹到B上 2.打开B上VisualSVN Server Manager ...

  6. 使用 rem 实现 适配各种屏幕布局

    年数已久!!!技术更新太快,谨慎观看,不过作为了解一下思路还是可以的 在此呢,我只大略的谈一下在研究了px,em,rem,和手淘的做法之后,我所采用的做法及硬性规则: 我就不再过多的将上面三种单位的区 ...

  7. PHP CodeIgniter(CI)去掉 index.php

    去掉CodeIgniter(CI)默认url中的index.php的步骤: 1.打开apache的配置文件,conf/httpd.conf : LoadModule rewrite_module mo ...

  8. R简易入门(二)

    本文内容来源:https://www.dataquest.io/mission/128/working-with-data-frames 本文摘要:简单介绍一下用R处理数据   原始数据展示(这是一份 ...

  9. django - transaction

    def user_atomic(): User.objects.create(name='purk1', email='pwu1@maxprocessing.com') User.objects.cr ...

  10. 每日一“酷”之Queue

    Queue—线程安全的FIFO实现 作用:提供一个线程安全的FIFO实现 Queue模块提供了一个适用于多线程编程的先进先出(first-in,first-out)数据结构,可以用来在生产者和消费者线 ...