No application found. Either work inside a view function or push an application context.
flask报了这个错,字面意思是说没有应用上下文,字面给的解决意见是要么放置在一个视图内,要么提供一个应用(flask)上下文.
查看文档发现文档给了个解决方案:
一个是通过app.app_context().push()来推入一个上下文,第二个是通过with上下文来确定作用在APP上下文区域内的代码.
个人觉得还是通过装饰器的方式来的方便和美观,当然第二种方式也相当优美.
下面是我的解决方法:
def sqlalchemy_context(app):
def add_context(func):
@wraps(func)
def do_job(*args, **kwargs):
app.app_context().push()
result = func(*args,**kwargs)
return result
return do_job
return add_context
然后我使用数据库的地方:
@sqlalchemy_context(APP)
def init_primary_key():
Model.query.filter_by()
...
* 我APP传入方式是因为避免循环导包, 思路是这样,实现方式自己把握好了.
然后问题就可以解决了.
No application found. Either work inside a view function or push an application context.的更多相关文章
- [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 ...
- 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'
问题: 说是create_all()的地方有问题,莫名其妙. 后来经过查资料,找出解决方法.附上代码如下:
- 【转】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: ...
- AssertionError: View function mapping is overwriting an existing endpoint function: admin.main
刚才给views.py文件添加了一个路由地址: @admin_view.route('/test', methods=["get", "post"]) @log ...
- django view function
view function 的几种返回值 return HttpResponse(html) return HttpResponseNotFound(html) raise Http404(" ...
- Flask之endpoint错误View function mapping is overwriting an existing endpoint function: ***
最近在学习Flask, 其中遇到了一个错误, 发现这个问题和Flask, 路由有关系, 所以就记了下来 错误代码: from flask import Flask, render_template, ...
- "AssertionError: View function mapping is overwriting an existing endpoint function"如何解决
使用Flask定义URL的时候,如果出现"AssertionError: View function mapping is overwriting an existing endpoint ...
随机推荐
- [doker]ubuntu18安装doker
ubuntu安装doker很简单,分4个步骤: Step1:更新资源库并安装apt-transprot-https软件包. 在安装Docker前, 首拉取最新的软件资源库 wangju@wangju- ...
- 远程连接mysql要点 虚拟主机定义与分类
远程连接mysql:通过主机地址与端口号连接 1. 主机地址:找到主机电脑 2. 端口号:找到对应mysql软件 mysql客户端访问服务端需要进行寻找匹配:连接认证-h 主机地址 例如 -hloca ...
- flutter GestureDetector点击区域
使用GestureDetector包裹Container,发现在Container内容为空的区域点击时,捕捉不到onTap点击事件. 解决方案:在GestureDetector里面添加属性: beha ...
- string中getline,cin的方法getline(),get总结
一.string中的getline不是string的成员函数,属于全局函数,使用需要include<string>,有两个重载版本: 函数原型参见:http://www.cplusplus ...
- 【HTTP】四、HTTP协议常见问题
HTTP协议是一个非常重要的应用层协议,在面试中有很多关于这方面的问题,这里做一个总结,大部分都在前面的文章中提到了,没提到的这里做一个介绍. 1.HTTP协议的基本原理.工作流程 HTTP协 ...
- GitLab使用小结
对Git和GitLab的使用作一个小结 GitLab基于Git,可以作为团队开发项目使用,因此通常会有一个主分支master和其他分支,因此项目成员中任意一人不能随意push到主分支中,容易引起混乱: ...
- python-爬取糗事百科热图
此次运用requests和beautifulSoup爬取糗事百科热图,常用的网络库有:urllib,urllib3,requests,选取其中之一就行了:HTML/XML解析器有:lxml,Beaut ...
- 【VS开发】【图像处理】RGB各种格式
RGB格式 RGB组合格式 名字 RGB组合格式 描述 此格式用来匹配PC图形帧缓存.每个像素占据8,16,24或32个位,他们都是组合像素格式,其意为在内存中所有像素数据都是相邻排列的.当使用这些格 ...
- vps分区 挂载wdcp 的/www目录大小调整或增加分区/硬盘的方法
http://www.wdlinux.cn/bbs/viewthread.php?tid=3574&highlight=%B7%D6%C7%F8 http://www.80vps.com/ne ...
- 快速乘+快速幂(用于模数超过int范围)
一般的快速幂并不适合模数大于int范围的情况,因为在乘法运算的过程可能会出现超出long long的情况出现.这个时候可以利用快速幂的思想使用快速乘,原理就是模拟乘法运算,将乘法运算分解成加法运算,再 ...