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 ...
随机推荐
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- 转载-Mysql主主复制架构配置
Mysql主主复制架构配置 转载:原始出处 http://luoweiro.blog.51cto.com/2186161/658550MySQL主主复制结构区别于主从复制结构.在主主复制结构中,两台服 ...
- Django模型的Field Types
Field Types 常用参数: null 如果设置为 True , Django 存放一个 NULL 到数据库字段.默认为 False. allow_null 如果设置为 True , 该字段将接 ...
- 我们公司的delphi代码(胆不是我写的!),看看,你觉得怎么样
unit unt_LotBill_dyc; interface uses windows, SysUtils, Classes, ComCtrls, Forms, Controls, StrUtils ...
- Day06:方法 / 猜字母游戏
JAVA方法 方法就是处理一个业务所需要编写的代码的代码段 方法特性 一个方法处理一个业务 方法代码编写,不和其他方法冲突 方法定义后可以随意调用 将main方法中的所有代码分散到各个普通方法中 减少 ...
- line-height 与 height 的区别
line-height是行高的意思,它决定了元素中文本内容的高度,height则是定义元素自身的高度. height:表示 行高 line-height:表示 每行文字所占的高度 举例: 第 ...
- docker 运行mysql最新版本用navicat连接报错:1251
主要是:新版的mysql的加密方式发生了变化,解决方法如下: 1:进入docker容器: docker exec -it mysql(启动mysql时候起的别名) /bin/bash 2:登陆my ...
- mariadb数据库——关联、视图、事务、索引、外键
1.关联 1)连接查询(内关联) inner join ... on 两个表连接查询 select * from students inner join classes 查询能够对应班级的学生以及班级 ...
- 【神经网络与深度学习】基于Windows+Caffe的Minst和CIFAR—10训练过程说明
Minst训练 我的路径:G:\Caffe\Caffe For Windows\examples\mnist 对于新手来说,初步完成环境的配置后,一脸茫然.不知如何跑Demo,有么有!那么接下来的教 ...
- 简述前后端项目RSA+AES加解密
一.登录机制 在项目中,我们可以大致得出一个登录的过程,主要分为 登录验证.登录保持.退出三个部分.登录验证是指客户端提供用户名和密码,向服务器提出登录请求,服务器判断客户端是否可以登录并向客户端确 ...