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 ...
随机推荐
- KVM 开启嵌套虚拟化
问题 在 CentOS KVM 上启动虚拟机来部署 OpenStack 测试环境,在启动具有 CPU 绑定.NUMA 亲和的虚拟机时触发错误: libvirtError: Requested oper ...
- Oracle自动性能统计
Oracle自动性能统计 高效诊断性能问题,需要提供完整可用的统计信息,好比医生给病人看病的望闻问切,才能够正确的确诊,然后再开出相应的药方.Oracle数据库为系统.会话以及单独的sql语句生成 ...
- 磁盘的分区和挂载(mount)
一.挂载问题的引入 我们大多数人用惯了windos系统,对linux系统中磁盘的管理就先入为主,不太好理解挂载这一动作.在linux系统中添加一块新磁盘后,要进行分区.格式化(分配文件系统).挂载.当 ...
- python基础--导入模块
一,import的使用1, 模块就是一组功能的集合体,我们的程序可以导入模块来复用模块中的功能一个模块就是包含了一组功能的python文件,例如demo.py 可以通过import来使用这个文件定义d ...
- lua基础学习(五)
一.Lua 模块与包 模块类似于一个封装库,从 Lua 5.1 开始,Lua 加入了标准的模块管理机制,可以把一些公用的代码放在一个文件里,以 API 接口的形式在其他地方调用,有利于代码的重用和降低 ...
- DataGridView中EnditCommit()调用之后,单元格的内容被全选了,每次输入都要鼠标点击定位到最后才能继续输入
因为某些需求,DataGridView在输入一次内容,就要调用ECommitEdit(DataGridViewDataErrorContexts.Commit)来将内容提交,但是这样做之后,控件就会当 ...
- Educational Codeforces Round 64 -B(贪心)
题目链接:https://codeforces.com/contest/1156/problem/B 题意:给一段字符串,通过变换顺序使得该字符串不包含为位置上相邻且在字母表上也相邻的情况,并输出. ...
- Hbase 三维存储
hbase所谓的三维有序存储的三维是指:rowkey(行主键),column key(columnFamily+qualifier),timestamp(时间戳)三部分组成的三维有序存储. 1.row ...
- springboot -- 2.0版本自定义ReidsCacheManager的改变
1. 问题发现 在1.0版本中,我们配置redis的cacheManager是这种方式: //缓存管理器 @Bean public CacheManager cacheManager(@Suppres ...
- [CF750G] New Year and Binary Tree Paths
目录 简单的 组合的 题目链接 简单的 设从节点\(x\)开始不断往左儿子走h-1步,则编号和为\(x\sum_{i=0}^{h-1}2^i=x(2^h-1)\). 若倒数第\(i\)步走向的是右儿子 ...