[flask初学问题]RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/
看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/的更多相关文章
- RuntimeError: No application found. Either work inside a view function or push an application context.
记录: 遇到这种报错信息: 在create_all()生成数据表的时候,添加app=app,指明app对象即可-----> create_all(app=app)
- No application found. Either work inside a view function or push an application context.
flask报了这个错,字面意思是说没有应用上下文,字面给的解决意见是要么放置在一个视图内,要么提供一个应用(flask)上下文. 查看文档发现文档给了个解决方案: 一个是通过app.app_conte ...
- 'No application found. Either work inside a view function or push'
问题: 说是create_all()的地方有问题,莫名其妙. 后来经过查资料,找出解决方法.附上代码如下:
- python 运行出现flask运行时提示出错了或者报服务器出错,ValueError: View function did not return a response
python manage.py runserver -d
- 【转】How to view word document in WPF application
How to view word document in WPF application (CSVSTOViewWordInWPF) Introduction The Sample demonstra ...
- 【Flask】报错解决方法:AssertionError: View function mapping is overwriting an existing endpoint function: main.user
运行Flask时出现了一个错误, AssertionError: View function mapping is overwriting an existing endpoint function: ...
- Flask之endpoint错误View function mapping is overwriting an existing endpoint function: ***
最近在学习Flask, 其中遇到了一个错误, 发现这个问题和Flask, 路由有关系, 所以就记了下来 错误代码: from flask import Flask, render_template, ...
- 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 ...
- flask 初学1
py 文件中 from flask import Flask,redirect,request,url_for,jsonifyfrom Flask_5.config import Config fro ...
随机推荐
- 使用kubeadm部署K8S v1.17.0集群
kubeadm部署K8S集群 安装前的准备 集群机器 172.22.34.34 K8S00 172.22.34.35 K8S01 172.22.34.36 K8S02 注意: 本文档中的 etcd . ...
- 获取radio点击事件
获取radio点击事件,不能用click(),而是用change(). $('input[name="options"]').change(function(){ console. ...
- sql复合索引使用和注意事项
1.定义: 单一索引: 单一索引是指索引列为一列的情况,即新建索引的语句只实施在一列上; 复合索引: 复合索引也叫组合索引: 用户可以在多个列上建立索引,这种索引叫做复合索引(组合索引). 复合索引在 ...
- DB2输出每隔10分钟的数据
一.输出1-100的数据 此处参考 https://bbs.csdn.net/topics/390516027 with t(id) as ( as id from sysibm.sysdummy1 ...
- sqlite 版本更新维护, 表结构判断, 更新
sqlite会自动维护一个系统表sqlite_master,该表存储了我们所创建的各个table, view, trigger等等信息. sqlite_master表数据字段: type: 类型,取值 ...
- 【贪心】洛谷2019 OI春令营 - 普及组 作业
[P3817 小A的糖果 小A有N个糖果盒,第i个盒中有a[i]颗糖果. 小A每次可以从其中一盒糖果中吃掉一颗,他想知道,要让任意两个相邻的盒子中加起来都只有x颗或以下的糖果,至少得吃掉几颗糖. [贪 ...
- 自动化测试之if __name__ == '__main__'未运行
自动化测试之if __name__ == '__main__'未运行 添加Count类 calculator.py: class Count: def __init__(self,a,b): self ...
- (十三)使用handler实现登录验证
一.Handel概念 J2EE Web 服务中的Handler技术特点非常像Servlet技术中的Filter.我们知道,在Servlet中,当一个HTTP到达服务端时,往往要经过多个Filter对请 ...
- LeetCode 754. Reach a Number
754. Reach a Number(到达终点数字) 链接:https://leetcode-cn.com/problems/reach-a-number/ 题目: 在一根无限长的数轴上,你站在0的 ...
- python 切换虚拟环境
每次电脑重启后,都要切入虚拟环境,命令总是忘记.如果使用IDE,可以指定interpeter方便的切换. 首先conda info --env 查看当前有几个环境 激活/禁用环境 source ac ...