看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. Jmeter 跨线程组传递参数 之两种方法(转)

    终于搞定了Jmeter跨线程组之间传递参数,这样就不用每次发送请求B之前,都需要同时发送一下登录接口(因为同一个线程组下的请求是同时发送的),只需要发送一次登录请求,请求B直接用登录请求的参数即可,直 ...

  2. 如何安装Oracle--新手安装Oracle教程

    1. 将win32_11gR2_database_1of2.zip与win32_11gR2_database_1of2.zip 解压到当前目录 PS:选中两个压缩包后右键解压到当前文件夹:必须同时解压 ...

  3. 安装jar包到本地仓库

    1.控制台安装 安装指定文件到本地仓库命令:mvn install:install-file-DgroupId=<groupId>       : 设置项目代码的包名(一般用组织名)-Da ...

  4. 强类型c语言 类型

    C 语言是一门强类型的语言,类型决定语言的特性,允许在相同类型进行运算. *强类型语言的特点:      1      先声明,后使用. 2      类型不可变. * 基本数据类型 * 数据类型的范 ...

  5. 关于typecho发布文章后的错位

    今天发布了一篇文章,发布后发现,what?主页错位了,安装控制变量法知道,肯定是这篇文章有什么不可告人的秘密. 所以,顺便使用一下二分法查找一下为啥,最后找到是因为使用了---------->( ...

  6. Nginx 路由重写

    很多时候我们的真实路由是隐藏的,都经过重写后展现到前台,下面简单写两个我经常用到的几个: 一般在配置*.host(在http里面引入的server配置)的时候会用到每个不同网址的路由重写,每一个rew ...

  7. Swiper 轮播插件 之 动态加载无法滑动

    1.原因:轮播图未完全动态加载完成,即初始化 2.方法一:ajax链式编程 $.ajax({ type: "get", url: serviceURL + "/listB ...

  8. Thymeleaf 模板使用 Error resolving template "/home", template might not exist or might not be accessible by any of the

    和属性文件中thymeleaf模板的配置相关 1.配置信息 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix= ...

  9. Linux 命令行:cURL 的十种常见用法

    Linux 命令行:cURL 的十种常见用法 文章目录 1. 获取页面内容 2. 显示 HTTP 头 3. 将链接保存到文件 4. 同时下载多个文件 5. 使用 -L 跟随链接重定向 6. 使用 -A ...

  10. UDP及操作系统理论

    UDP介绍 udp协议又称用户数据报协议 在OSI七层模型中,它于TCP共同存在于传输层 仅用于不要求可靠性,不要求分组顺序且数据较小的简单传输,力求速度 UDP结合socket用法 1.创建sock ...