当前位置:   article > 正文

Flask中如何在蓝图中使用sqlalchemy_flask sqlalchemy 在 蓝图中的使用

flask sqlalchemy 在 蓝图中的使用

 

原文地址:https://www.cnblogs.com/diaosir/p/6351721.html

主要是要注意引入蓝图的位置一定要是在初始化db之后才行;

下程序结构:

1.建立mysql和app的连接

在config.py中加入以下两项配置:

SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:xxxxx@localhost:3306/test?charset=utf8'
SQLALCHEMY_TRACK_MODIFICATIONS = True

如此在app/__init__.py中加入
app.config.from_object('config')
db = SQLAlchemy(app)
就可以完成app和数据的关联,并生成一个可以操作app数据库的SQLAlchemy实例db

蓝图的引入要放在初始化DB之后,不然会报错;

 

完整的app/__init__.py代码如下:
  1. from flask import Flask, url_for, request, redirect, render_template
  2. from flask_sqlalchemy import SQLAlchemy
  3. app = Flask(__name__)
  4. app.config.from_object('config')
  5. db = SQLAlchemy(app)
  6. # 这里引入蓝图的配置
  7. from app import models,views

接下来就是主要的两个蓝图,以及他们的注册

#app/user.py

  1. from flask import Blueprint, render_template, redirect,request
  2. from app import db
  3. from .models import User
  4. user = Blueprint('user',__name__)
  5. @user.route('/index')
  6. def index():
  7. return render_template('user/index.html')
  8. @user.route('/add/',methods=['GET','POST'])
  9. def add():
  10. if request.method == 'POST':
  11. p_user = request.form.get('username',None)
  12. p_email = request.form.get('email',None)
  13. p_password = request.form.get('password',None)
  14. if not p_user or not p_email or not p_password:
  15. return 'input error'
  16. newobj = User(username=p_user, email=p_email, password=p_password)
  17. db.session.add(newobj)
  18. db.session.commit()
  19. users = User.query.all()
  20. return render_template('user/add.html',users=users)
  21. users = User.query.all()
  22. return render_template('user/add.html',users=users)
  23. @user.route('/show')
  24. def show():
  25. return 'user_show'

#app/admin.py

  1. #admin.py
  2. from flask import Blueprint,render_template, request, redirect
  3. from app import db
  4. from .models import Admin
  5. admin = Blueprint('admin',__name__)
  6. @admin.route('/index')
  7. def index():
  8. return render_template('admin/index.html')
  9. @admin.route('/add/',methods=['POST','GET'])
  10. def add():
  11. if request.method == 'POST':
  12. p_admin = request.form.get('username',None)
  13. p_email = request.form.get('email',None)
  14. p_password = request.form.get('password',None)
  15. if not p_admin or not p_email or not p_password:
  16. return 'input error'
  17. newobj = Admin(username=p_admin, email=p_email, password=p_password)
  18. db.session.add(newobj)
  19. db.session.commit()
  20. admins = Admin.query.all()
  21. return render_template('admin/add.html',admins=admins)
  22. admins = Admin.query.all()
  23. return render_template('admin/add.html',admins=admins)
  24. @admin.route('/show')
  25. def show():
  26. return 'admin_show'

 

#app/views.py

  1. from app import app
  2. from .admin import admin
  3. from .user import user
  4. app.register_blueprint(admin,url_prefix='/admin')
  5. app.register_blueprint(user, url_prefix='/user')

#run.py

  1. from app import app
  2. app.run()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/501869
推荐阅读
相关标签
  

闽ICP备14008679号