一区匠、模型
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:123456@127.0.0.1:3306/tornado9'
# 創(chuàng)建引擎建立連接
engine = create_engine(db_url)
# 模型與數(shù)據(jù)庫表關(guān)聯(lián)的基類驰弄,模型必須繼承于Base
Base = declarative_base(bind=engine)
# 創(chuàng)建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
# 設(shè)置默認端口
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)
],
# 指定參數(shù)表示文件在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()
# 監(jiān)聽端口
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
# 練級數(shù)據(jù)庫格式
# mysql+pymysql://root:123456@127.0.0.1:3306/tornado9
db_url = 'mysql+pymysql://root:123456@127.0.0.1:3306/tornado9'
# 創(chuàng)建引擎建立連接
engine = create_engine(db_url)
# 模型與數(shù)據(jù)庫表關(guān)聯(lián)的基類,模型必須繼承于Base
Base = declarative_base(bind=engine)
# 創(chuàng)建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
# 連接數(shù)據(jù)庫格式
# mysql+pymysql://root:123456@127.0.0.1:3306/tornado9
db_url = 'mysql+pymysql://root:123456@127.0.0.1:3306/tornado9'
# 創(chuàng)建引擎建立連接
engine = create_engine(db_url)
# 模型與數(shù)據(jù)庫表關(guān)聯(lián)的基類揩懒,模型必須繼承于Base
Base = declarative_base(bind=engine)
# 創(chuàng)建session會話
DbSession = sessionmaker(bind=engine)
session = DbSession()
二什乙、HTTP行為方法
方法 | 說明 |
---|---|
GET | 用于獲取數(shù)據(jù) |
POST | 用于創(chuàng)建數(shù)據(jù) |
PUT | 修改某個對象的全部屬性 |
PATCH | 修改某個對象的部分屬性 |
DELETE | 刪除數(shù)據(jù) |
三、模板
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有區(qū)別。
4忆某、繼承{% extends '父模板名稱' %}
5点待、模板語法
{# 我是注釋! #}
{% block content %}
<p>(??ˇ?ˇ?)</p>
<select>
{% for item in items %}
<option {% if item == "Php" %} selected {% end %}>{{ item }}</option>
{% end %}
</select>
{% for score in scores %}
{% if score > 60 %}
<p style="color:red;">{{ score }}</p>
{% elif score < 10 %}
<p style="color:green;">{{ score }}</p>
{% else %}
<p>{{ score }}</p>
{% end %}
{% end %}
{% while len(items) %}
<p>{{ items.pop() }}</p>
{% end %}
{% try %}
<p>{{ item[10] }}</p>
{% except %}
<p>下標越界</p>
{% finally %}
<p>必須執(zhí)行操作</p>
{% end %}
{% set n=1 %}
<p>{{ n }}</p>
{% end %}
?