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 ...
随机推荐
- DateTimePicker 日期时间选择器
在同一个选择器里选择日期和时间 DateTimePicker 由 DatePicker 和 TimePicker 派生,Picker Options 或者其他选项可以参照 DatePicker 和 T ...
- Win10 安装LoadRunner11遇到的问题及解决方案
由于以用户或者管理员身份执行setup.exe都不能正常安装,如下截图是异常信息.尝试了网上很多修改本地组策略的方法,还是不行,最后只能通过DOS命令来执行setup.exe.
- UML学习笔记_01_基本概念
1.什么是UML Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是始于1997年一个OMG标准,它是一个支持模型化和软件系统开发的图形化语言,为软件开发 ...
- Elasticsearch 6.2.3版本 string 类型字段 排序 报错 Fielddata is disabled on text fields by default
背景说明 最近在做一个 Elasticsearch 的分页查询,并且对查询结果按照特定字段进行排序的功能. 但是执行结果却报错,报错信息如下: { "error": { " ...
- 微信小程序的生命周期和APP对象的使用
1.生命周期和APP对象的使用: //app.js App({ onLaunch: function () { //调用API从本地缓存中获取数据 var logs = wx.getStorageSy ...
- JMeter强大的性能测试工具
JMeter强大的性能测试工具,可模拟服务器负载,进行性能测试 配合badboy采集请求数据.
- Nginx的用途
Nginx应该是现在最火的web和反向代理服务器,没有之一.她是一款诞生于俄罗斯的高性能web服务器,尤其在高并发情况下,相较Apache,有优异的表现. 那除了负载均衡,她还有什么其他的用途呢,下面 ...
- aliyun搭博客从零到一
一.基础环境 lnmp 1台负载均衡SLB 2台ECS 1台 RDS 二.lnmp搭建 1.#配置nginx的yum仓库 2.#yum install -y nginx ...
- 【Deep Learning Nanodegree Foundation笔记】第 9 课:Model Evaluation and Validation
In this lesson, you'll learn some of the basics of training models. You'll learn the power of testin ...
- 浅谈spring配置定时任务的几种方式
网上看到好多关于定时任务的讲解,以前只简单使用过注解方式,今天项目中看到基于配置的方式实现定时任务,自己做个总结,作为备忘录吧. 基于注解方式的定时任务 首先spring-mvc.xml的配置文件中添 ...