[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 ...
随机推荐
- go基础系列 第一章 go基础语法
0.前言 1. go定义变量的几种方式 2. go内建变量类型 3. 常量的定义 4. go枚举 5. go的if语句 零. go语言的换行 go语言对换行很有讲究, 如果想换行,必须有一个逗号, 否 ...
- python代码注释 - python基础入门(4)
在 python改变世界,从hello world开始 中我们已经完成了第一个python程序,代码是有了,关键是好像好不知道写的啥玩意? 一.什么是代码注释 代码注释就是给一段代码加上说明,表明这段 ...
- SDOI2010_大陆争霸(邻接表存图)
题目描述 在一个遥远的世界里有两个国家:位于大陆西端的杰森国和位于大陆东端的 克里斯国.两个国家的人民分别信仰两个对立的神:杰森国信仰象征黑暗和毁灭 的神曾·布拉泽,而克里斯国信仰象征光明和永恒的神斯 ...
- C++Primer 5th Chap5 Statements
else语句对应的始终是最近的那条if语句,除非有{}强行控制,如: if(A){ if(B){/*.............*/} }else{/*.......*/}//这里else和if(A)对 ...
- 前端开发 — HTML
HTML HTML 超文本标记语言 HTML特征: 对换行和空格不敏感 空白折叠 1.1 HTML标签 标签也称为标记. 标签的种类: 1.双闭合标签 2.单闭合标签 1.1.1 head标签 met ...
- 1192: 零起点学算法99——The sum problem(C)
一.题目 http://acm.wust.edu.cn/problem.php?id=1192&soj=0 二.分析 要求从序列1,2,3,,,N,中截取一部分使他们的和为M 输入多组数据 输 ...
- PHP的 parse_ini_file 解析配置文件
解析配置文件: parse_ini_file 类似解析php.ini文件样 配置文件内容如下: Example #1 sample.ini 的内容 ; This is a sample configu ...
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Coin 矩阵快速幂
Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face u ...
- C#工厂模式案例
class JianDanGongChang { static void Main(string[] args) { Factory factory=new LianXiangFactory(); D ...
- ASP.NET WEB应用程序(.network4.5)MVC 工作原理
MVC就是模型.视图.控制器. 项目中控制器对应Controllers目录,视图对应Views目录,模型对应Models目录. 1.当我们创建一个控制器时,比如在Controllers目录新建一个名字 ...