基于包结构的模块导入

1、创建项目目录----------->OA

2、在项目目录下创建 包project----->app目录

3、在app目录下创建__init__.py包文件,views.py , main.py,models.py

flask -- 基于包结构的模块导入

flask -- 基于包结构的模块导入flask -- 基于包结构的模块导入flask -- 基于包结构的模块导入

 

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

1、在__init__.py 中

from flask import Flask  
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)  # 创建app功能模块  
# 设置路径,用于连接数据库
app.config["SQLALCHEMY_DATABASE_URI"]='sqlite:////project/djangoproject/OA/flask.db'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"]=True  # 追踪对象的修改并且发送信号

db = SQLALchemy(app)  #根据app应用实例化数据库

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

2、在views.py中

from project import app
@app.route('/')  # 装饰器,指定路由
def index():  # 定义一个函数
    return 'hello world'

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

3、在main.py 中

from project.views import app
command = input(">>>>")
if command == "runserver":
    app.run()   # 运行启动服务器

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4、在models.py 中

from project import db
 # 导入实例化的数据库,创建表结构
class Student(db.Model):   
    id =db.Column(db.Integer,primary_key = True)
    name = db.Column(db.String(32))
    age = db.Column(db.Integer)
    gender = db.Column(db.String(32))
    classes = db.Column(db.String(32))
    
    def __repr__(self):
        return self.name

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

5、在main.py 中

from project.views imort app
from project.models import db

command = input(">>>")
if command == "runserver":
    app.run()
elif command == "migrate":
    db.create_all()   # 同步数据库 

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

6、运行main.py ,输入migrate ,同步数据库

flask -- 基于包结构的模块导入

成功:

flask -- 基于包结构的模块导入

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

7、在views.py中,在这里实现对数据库的增删改查,注意增删改一定要记得提交数据库。

from project import app 
from projrct.models import Student
from project.models import db 

session = db.session 
@app.route('/')
def index():
    # 增加
    student = Student(name="老王",age="18",gender="男",classes = "python")
    session.add(student)
    session.commit()
    return 'hello world'

增加


v = Student(name="老王",age="18",gender="女",classes="python")
s = Student(name="老李",age="16",gender="男",classes="python")
q = Student(name="老刘",age="33",gender="女",classes="python")
n = Student(name="老赵",age="10",gender="男",classes="Java")
p = Student(name="老张",age="18",gender="男",classes="Linux")
session.add_all([v, s, q, n, p])
session.commit()

删除

student = Student.query.get(1)
db.session.delete(student)
session.commit()

修改

修改
student = Student.query.get(2)
student.name = "小姐"
session.commit()

查看

students = Student.query.all() # 查询所有
students = Student.query.filter_by(age=18)  # 按条件查询
student= Student.query.get(1) # 自动以主键查询
students = Student.query.group_by("age")  # 按照组查询
students = Student.query.order_by(Student.age) # 排序
students = Student.query.order_by(Student.age.desc()) # 倒排序

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

8、在main.py中运行,输入runserver ,开启服务器。

flask -- 基于包结构的模块导入

 

 

相关文章:

  • 2021-08-18
  • 2022-12-23
  • 2021-10-28
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-03
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
相关资源
相似解决方案