Day2 tornado模型与模板

一、模型

1、准备
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String

from sqlalchemy.orm import sessionmaker
2、配置
db_url = 'mysql+pymysql://root:[email protected]:3306/tornado9'

# 创建引擎建立连接
engine = create_engine(db_url)

# 模型与数据库表关联的基类,模型必须继承于Base
Base = declarative_base(bind=engine)

# 创建session会话
DbSession = sessionmaker(bind=engine)
session = DbSession()

def create_db():
    # 映射模型对应的表
    Base.metadata.create_all()

def drop_db():
    # 删除模型映射表
    Base.metadata.drop_all()

class Student(Base):
    id = Column(Integer, primary_key=True, autoincrement=True)
    s_name = Column(String(10), unique=True, nullable=False)
    s_age = Column(Integer, default=18)

    __tablename__ = 'student'
3、使用
#增
stu = Student()
stu.s_name = 'xiaoming'
stus.append(stu)
session.add_all(stus)
session.commit()

#删
session.delete(stu)
session.commit()

#改
stu.s_name = 'xiaohua'
session.add(stu)
session.commit()

#查
#方法一:
stu = session.query(Student).filter(Student.s_name == 'xiaoming_1').first()
#方法二:
stus = session.query(Student).filter_by(s_name = 'xiaoming_0').all()

manage.py

import os

import tornado.web
import tornado.ioloop
from tornado.options import define, options, parse_command_line

from app.views import IndexHandler, XindexHandler, DbHandler, DropDbHandler, AddStuHandler, StusHandler

# 设置默认端口
define('port', default=80, type=int)


def make_app():
    return tornado.web.Application(handlers=[
        (r'/', IndexHandler),
        (r'/xindex/', XindexHandler),
        (r'/init_db/', DbHandler),
        (r'/drop_db/', DropDbHandler),
        (r'/add_stu/', AddStuHandler),
        (r'/stus/', StusHandler)
    ],
    # 指定参数表示文件在templates
    template_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates'),
    static_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static'))



if __name__ == '__main__':
    # 解析命令行
    parse_command_line()
    # 生成Application对象
    app = make_app()
    # 监听端口
    app.listen(options.port)
    # 启动
    tornado.ioloop.IOLoop.current().start()

app/models.py

from sqlalchemy import Column, Integer, String

from utils.conn import Base

def create_db():
    # 映射模型对应的表
    Base.metadata.create_all()

def drop_db():
    # 删除模型映射表
    Base.metadata.drop_all()

class Student(Base):
    id = Column(Integer, primary_key=True, autoincrement=True)
    s_name = Column(String(10), unique=True, nullable=False)
    s_age = Column(Integer, default=18)

    __tablename__ = 'student'

utils/conn.py


from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import sessionmaker

# 练级数据库格式
# mysql+pymysql://root:[email protected]:3306/tornado9

db_url = 'mysql+pymysql://root:[email protected]:3306/tornado9'

# 创建引擎建立连接
engine = create_engine(db_url)

# 模型与数据库表关联的基类,模型必须继承于Base
Base = declarative_base(bind=engine)

# 创建session会话
DbSession = sessionmaker(bind=engine)
session = DbSession()


app/views.py

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import sessionmaker

#  连接数据库格式
# mysql+pymysql://root:[email protected]:3306/tornado9

db_url = 'mysql+pymysql://root:[email protected]:3306/tornado9'

# 创建引擎建立连接
engine = create_engine(db_url)

# 模型与数据库表关联的基类,模型必须继承于Base
Base = declarative_base(bind=engine)

# 创建session会话
DbSession = sessionmaker(bind=engine)
session = DbSession()

二、HTTP行为方法

方法 说明
GET 用于获取数据
POST 用于创建数据
PUT 修改某个对象的全部属性
PATCH 修改某个对象的部分属性
DELETE 删除数据

三、模板

1、模板文件路径定义:Application(handlers=[], template_path='指定template文件夹的路径')

2、获取templates路径 os.patch.join(os.path.dirname(os.path.abspath(__file__)), 'templates')

3、挖坑 {% block name %} {% end %}注意:这里和Flask的jinja2有区别。

4、继承{% extends '父模板名称' %}

5、模板语法

{# 我是注释! #}
{% block content %}
    

(。◕ˇ∀ˇ◕)

{% for score in scores %} {% if score > 60 %}

{{ score }}

{% elif score < 10 %}

{{ score }}

{% else %}

{{ score }}

{% end %} {% end %} {% while len(items) %}

{{ items.pop() }}

{% end %} {% try %}

{{ item[10] }}

{% except %}

下标越界

{% finally %}

必须执行操作

{% end %} {% set n=1 %}

{{ n }}

{% end %}

你可能感兴趣的:(Day2 tornado模型与模板)