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.的更多相关文章

  1. [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 ...

  2. RuntimeError: No application found. Either work inside a view function or push an application context.

    记录: 遇到这种报错信息: 在create_all()生成数据表的时候,添加app=app,指明app对象即可-----> create_all(app=app)

  3. 'No application found. Either work inside a view function or push'

    问题: 说是create_all()的地方有问题,莫名其妙. 后来经过查资料,找出解决方法.附上代码如下:

  4. 【转】How to view word document in WPF application

    How to view word document in WPF application (CSVSTOViewWordInWPF) Introduction The Sample demonstra ...

  5. 【Flask】报错解决方法:AssertionError: View function mapping is overwriting an existing endpoint function: main.user

    运行Flask时出现了一个错误, AssertionError: View function mapping is overwriting an existing endpoint function: ...

  6. AssertionError: View function mapping is overwriting an existing endpoint function: admin.main

    刚才给views.py文件添加了一个路由地址: @admin_view.route('/test', methods=["get", "post"]) @log ...

  7. django view function

    view function 的几种返回值 return HttpResponse(html) return HttpResponseNotFound(html) raise Http404(" ...

  8. Flask之endpoint错误View function mapping is overwriting an existing endpoint function: ***

    最近在学习Flask, 其中遇到了一个错误, 发现这个问题和Flask, 路由有关系, 所以就记了下来 错误代码: from flask import Flask, render_template, ...

  9. "AssertionError: View function mapping is overwriting an existing endpoint function"如何解决

    使用Flask定义URL的时候,如果出现"AssertionError: View function mapping is overwriting an existing endpoint ...

随机推荐

  1. 自定义ListView实现下拉刷新,下拉加载的功能

    package com.loaderman.myrefreshlistviewdemo; import android.content.Context; import android.util.Att ...

  2. 阶段3 2.Spring_03.Spring的 IOC 和 DI_12 注入集合数据

    再复制一份,改名3 常用的注入方式,这里选择set saveAccount方法输出所有的对象 map需要导包 配置xml 集合类型的值配置方式 在property标签里面再写标签 这里选择array. ...

  3. SO BAPI SD_SALESDOCUMENT_CREATE

    DATA: E_ORDER_HEADER_IN  LIKE  BAPISDHD1 OCCURS 0 WITH HEADER LINE,           E_ORDER_PARTNERS   LIK ...

  4. Python字符和字符值(ASCII或Unicode码值)转换方法

    Python字符和字符值(ASCII或Unicode码值)转换方法 这篇文章主要介绍了Python字符和字符值(ASCII或Unicode码值)转换方法,即把字符串在ASCII值或者Unicode值之 ...

  5. Web05_jQuery

    在官方网站下载包,下载不带有min的包 http://jquery.com/download/ 案例一:使用JQ完成首页定时弹出广告图片 01_JQ入门 01_jQuery入门.html <!D ...

  6. 获取重复字符串的range,设置attributedText

    之前项目的需求是未读人员显示绿色,已读人员显示黑色,测试今天提bug说存在未读人员显示的黑色...这就尴尬了,我完全不知道为啥,经过打断点调试程序,终于找到问题:就是他只会获取到第一个想等字符的ran ...

  7. java:Mybatis框架2(基于mapper接口的开发,多种查询,复合类型查询,resultMap定义,多表联查,sql片段)

    1.mybatis02: mybatis-config.xml: <?xml version="1.0" encoding="UTF-8"?> &l ...

  8. git 新建项目的一些操作

    Command line instructions Git global setup git config --global user.name "Administrator" g ...

  9. php配置 php-cgi.sock使用

    PHP配置文件: [global]pid = /run/php-fpm/php-fpm.piderror_log = /var/log/php-fpm/php-fpm.loglog_level = n ...

  10. [转帖]中国AI芯“觉醒”的五年

    中国AI芯“觉醒”的五年 https://www.cnbeta.com/articles/tech/857863.htm 原来 海思的营收已经超过了按摩店(AMD) 没想到.. 十多款芯片问世,多起并 ...