[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 ...
随机推荐
- 【ARM-Linux开发】Rico Board上编译USB WiFi RT3070驱动
1.附件中提供了RT3070驱动源码包DPO_RT5572_LinuxSTA_2.6.1.3_20121022.tar.gz和编译好的驱动,这里选择使用taget_file.tar.gz中已经编译好的 ...
- Java基础系列-substring的原理
原创文章,转载请标注出处:https://www.cnblogs.com/V1haoge/p/10755235.html JDK 6和JDK 7中substring的原理及区别 substring(i ...
- 网站架构(LNMP)
说明: 1)LNMP架构的组成(作用) 2)LNMP架构的部署 3)LNMP架构通讯原理 4)LNMP服务之间如何建立管理 nginx+php 运维负责 php+mysql 开发负责 5) 运 ...
- 乐字节Java变量与数据结构之二:Java常量与变量
大家好,小乐又来给大家讲述Java基础知识.上次说了乐字节Java变量与数据类型之一:Java编程规范,关键字与标识符,这次将接着往下说,谈谈Java常量与变量. 1.常量 1).定义 在程序执行的过 ...
- CNN-4: GoogLeNet 卷积神经网络模型
1.GoogLeNet 模型简介 GoogLeNet 是2014年Christian Szegedy提出的一种全新的深度学习结构,该模型获得了ImageNet挑战赛的冠军. 2.GoogLeNet 模 ...
- XML基础知识归纳(通俗易懂)
XML:可扩展标记型语言 随着json等一些技术的普及,似乎xml的路子越来越窄,虽然xml的一些功能被其他的一些技术代替,但是学习xml还是非常有必要,如果用xml存储一些大量数据,还是有一定优势的 ...
- NationalInstruments project
//using NationalInstruments.NI4882; //请将项目文件中的"AutoGenerateBindingRedirects"属性设置为true //ht ...
- 数据结构 -- 链表(LinkedList)
链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成. 每个结点包括两个部分 ...
- pandas之聚合运算
通过聚合运算可以得到我们比较感兴趣的数据以方便处理 import pandas as pd import numpy as np # 先创建一组数据表DataFrame df = pd.DataFra ...
- element-ui 时间设置 获取固定的时间格式
<el-date-picker v-model="time1" type="daterange" start-placeholder="开始日期 ...