看B站视频学习flask-SQLalchemy时,报错RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/

视频链接是https://bilibili.com/video/av19817183?p=20

P20 04-03数据库的基本操作1-增删改

位置3分17左右

以下是视频中说到的代码:

 from flask import Flask
from flask_sqlalchemy import SQLAlchemy app = Flask(__name__)
# 配置数据库的地址
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:emperor12@127.0.0.1/flask_sql_demo'
# 跟踪数据库的修改 --> 不建议开启,未来的版本中会删除
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy() # 数据库的模型,需要继承db.Model
class Role(db.Model):
# 定义表名
__tablename__ = 'roles'
# 定义字段
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(16),unique=True)
class Users(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer,primary_key = True)
name = db.Column(db.String(16),unique=True)
# db.ForeignKey('roles.id') 表示是外键。表名.id
role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) @app.route('/')
def hello_world():
return 'Hello World!' if __name__ == '__main__':
# 删除表
db.drop_all()
# 创建表
db.create_all()
app.run(debug = True)

按照视频弹幕的指引,先是在第一行import pymysql,但是不知道为什么显示是灰色的

然后pip install mysql-connector

或者是在cmd中net start mysql57

还是按照pycharm提示中https://flask-sqlalchemy.palletsprojects.com/en/2.x/contexts/中说的

def create_app():
app = Flask(__name__)
db.init_app(app)
return app

都没有效果

最后看了https://blog.csdn.net/zhongqiushen/article/details/79162792

在第9行把

db = SQLAlchemy()  改为   db = SQLAlchemy(app)

就可以了,我也没有明白为什么

[flask初学问题]RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/的更多相关文章

  1. RuntimeError: No application found. Either work inside a view function or push an application context.

    记录: 遇到这种报错信息: 在create_all()生成数据表的时候,添加app=app,指明app对象即可-----> create_all(app=app)

  2. No application found. Either work inside a view function or push an application context.

    flask报了这个错,字面意思是说没有应用上下文,字面给的解决意见是要么放置在一个视图内,要么提供一个应用(flask)上下文. 查看文档发现文档给了个解决方案: 一个是通过app.app_conte ...

  3. 'No application found. Either work inside a view function or push'

    问题: 说是create_all()的地方有问题,莫名其妙. 后来经过查资料,找出解决方法.附上代码如下:

  4. python 运行出现flask运行时提示出错了或者报服务器出错,ValueError: View function did not return a response

    python manage.py runserver -d

  5. 【转】How to view word document in WPF application

    How to view word document in WPF application (CSVSTOViewWordInWPF) Introduction The Sample demonstra ...

  6. 【Flask】报错解决方法:AssertionError: View function mapping is overwriting an existing endpoint function: main.user

    运行Flask时出现了一个错误, AssertionError: View function mapping is overwriting an existing endpoint function: ...

  7. Flask之endpoint错误View function mapping is overwriting an existing endpoint function: ***

    最近在学习Flask, 其中遇到了一个错误, 发现这个问题和Flask, 路由有关系, 所以就记了下来 错误代码: from flask import Flask, render_template, ...

  8. Flask - 访问返回字典的接口报错:The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict.

    背景 有一个 Flask 项目,然后有一个路由返回的是 dict 通过浏览器访问,结果报错 关键报错信息 TypeError: 'dict' object is not callable The vi ...

  9. flask 初学1

    py 文件中 from flask import Flask,redirect,request,url_for,jsonifyfrom Flask_5.config import Config fro ...

随机推荐

  1. MySQL5.7.9安装与配置优化

    一. 环境准备 1. 下载软件包 wget http://test.hexin.cn/software/mysql-5.7.9.tar.gz -P /usr/local/src/ wget http: ...

  2. UIPath工具里面如何入力一览里面的数据

    问题描述: UIpath工具如何在网页里面入力一览多条的数据.例如:某个公司里面在自己的内部员工管理的软件里面手动入力公司员工的所有信息之后反馈到数据库当中. 解决方法: UIpath工具来入力数据, ...

  3. Leetcode 224/227/772 计算器

    题目描述 Leetcode 224 Leetcode 224: 这里想让我们实现一个基础的计算器,来计算给定的字符串. 给定的字符串中包含 ( ) + - 和非负整数和空格. # Example 1: ...

  4. 洛谷 题解 UVA10048 【噪音恐惧症 Audiophobia】

    [题意] 输入一个\(C\)个点\(S\)条边 \((C<=100)\) \((S<=1000)\)的无向带权图,边权表示该路径上的噪声值.当噪声太大时,耳膜可能会收到损伤,所以当你从某点 ...

  5. 国产龙芯服务器源码安装PostgreSQL数据库的方法

    1. 公司最近有一些国产化项目的需求, 要求在国产CPU的服务器上面安装pg数据库等. 2.. 但是差查了下中标麒麟的官网,在龙芯MIPS的操作系统包源里面仅有 postgreSQL 9.2 版本的r ...

  6. linux下安装lnmp集成环境

    linux下安装lnmp集成环境 教程地址:https://www.cnblogs.com/peteremperor/p/6750204.html 必须要用root用户,否则权限不够无法安装 安装最新 ...

  7. Python习题005

    作业一 :任意一个数字列表,然后进行排序(冒泡排序) 方法一: def test1(): list1 = [1,23,4,6,8,55,2,9,90,35] list1.sort() # sort() ...

  8. WinRAR 去广告的姿势

    一直在使用WinRAR解压文件,感觉非常的好用,可是现在WinRAR添加了广告,每次打开压缩包都会弹出广告,有时候甚至在解压的时候弹出来,而每次弹出广告都会卡顿一下,忍了很长时间今天实在是受够了,准备 ...

  9. 2019杭电多校二 L. Longest Subarray (线段树)

    大意: 给定序列$a$, 元素范围$[1,C]$, 求一个最长子序列, 满足每个元素要么不出现, 要么出现次数$\le K$. 枚举右端点, 考虑左端点合法的位置. 显然一定是$C$种颜色合法位置的交 ...

  10. Once in a casino CodeForces - 1120B (暴力)

    大意: 给定两个字符串$a,b$, 每个字符为$0-9$, 每次操作将$a$中相邻两位加$1$或减$1$, 操作后每个数仍要为$0-9$, 求最少操作使$a$变成$b$. 先不考虑范围, 判断是否成立 ...