#importo mi app de mi archivo from hello import app from flask import current_app current_app.name #asi se obtiene el contexto de la aplicacion app_ctx = app.app_context() #corro el contexto de la aplicacion haciendola activa y ya puedo usar la instancia de current_app app_ctx.push() current_app.name app_ctx.pop()
from flask_mail import Message from hello import app,mail msg = Message('test subject',sender='*****@*****.**',recipients=['*****@*****.**']) msg.body = 'text body' msg.html = '<b>HTML</b> body' #mail.send(msg) with app.app_context(): mail.send(msg)
from flask.ext.mail import Message from hello import mail from hello import app msg = Message('test subject', sender='*****@*****.**', recipients=['*****@*****.**']) msg.body = 'text body' msg.html = '<b>HTML</b> body' with app.app_context(): mail.send(msg)
>>> from hello import app >>> from flask import current_app >>> current_app.name Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\Python\Flask\venv\lib\site-packages\werkzeug\local.py", line 348, in __getattr__ return getattr(self._get_current_object(), name) File "D:\Python\Flask\venv\lib\site-packages\werkzeug\local.py", line 307, in _get_current_object return self.__local() File "D:\Python\Flask\venv\lib\site-packages\flask\globals.py", line 52, in _find_app raise RuntimeError(_app_ctx_err_msg) RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed to interface with the current application object in some way. To solve this, set up an application context with app.app_context(). See the documentation for more information. >>> app_ctx = app.app_context() >>> app_ctx.push() >>> current_app.name 'hello' >>> app_ctx.pop() >>> app.url_map Map([<Rule '/' (OPTIONS, HEAD, GET) -> index>, <Rule '/static/<filename>' (OPTIONS, HEAD, GET) -> static>]) 1, 4,使用Flask-Script支持命令行选项 from flask import Flask app = Flask(__name__)
__author__ = 'root' from hello import app from flask import current_app app_ctx = app.app_context() app_ctx.push() print current_app.name print app.url_map app_ctx.pop()
#! /usr/bin/env pthon3 # -*- coding:utf-8 -*- __author__ = 'Aaron_chan' from hello import app from flask import current_app #print(current_app.name) app_cntx = app.app_context() app_cntx.push() print(current_app.name) print(app.url_map)
#coding=utf8 from hello import app from flask import current_app #print current_app.name app_ctx = app.app_context() #获得程序上下文 app_ctx.push() #激活程序上下文 print current_app.name app_ctx.pop() #上下文全局变量: #current_app 当前激活程序的程序实例 (程序上下文) #g 处理请求时用作临时存储的对象,每次请求都会重设这个变量(程序上下文) #request 请求对象,封装了http请求中的内容 (请求上下文) #session 用户会话,用于存储请求之间需要“记住”的值得字典 (请求上下文) #url映射:url和视图函数之间的对应关系 print app.url_map