2016-08-13 6 views
0

メインファイルから呼び出すconfig.pyモジュールがあります.jinja2filters用のさまざまなapp.configsと、使用している他のプラグインがあります。configモジュール用の "RuntimeError:アプリケーションコンテキスト外での作業"

config.pyの抜粋:

from flask import current_app as app 
#imports go here 

def function1: 
    print("hello") 

app.config['PROPERTY_1'] = 'configgoeshere' 

#jinja2 functions and configs would go here like 
app.jinja_env.filters['datetime'] = datetimeformat 

がindex.pyから呼び出す:

from flask import Flask, flash, render_template, jsonify, request, json, request, redirect, url_for, Response, send_from_directory 

app = Flask(__name__) 
app.config['DEBUG'] = False 

app.config.from_object('config') #config file 

############################################################################################################################ 
# MAIN ROUTES 
############################################################################################################################ 
# The route for the homepage 
@app.route('/') 
def index(): 

はエラーを返します:

RuntimeError: working outside of application context 

更新 私はここに、今に沿ってアプリケーション・コンテキストを渡すためにしようとしているがindex.pyです:

from flask import Flask, flash, render_template, jsonify, request, json, request, redirect, url_for, Response, send_from_directory 
import time, datetime, os, urllib, urllib2, urlparse, requests, json, string 
from urllib2 import Request, urlopen, URLError, HTTPError, urlparse 

app = Flask(__name__) 
app.config['DEBUG'] = False 

app.config.from_object('config') 


############################################################################################################################ 
# MAIN ROUTES 
############################################################################################################################ 
# The route for the homepage 
@app.route('/') 
def index(): 

ここで更新抜粋config.pyです:

from index import app 
#imports go here 

def function1: 
    print("hello") 

app.config['PROPERTY_1'] = 'configgoeshere' 

#jinja2 functions and configs would go here like 
app.jinja_env.filters['datetime'] = datetimeformat 

返されるエラー:

$ python index.py 
Traceback (most recent call last): 
    File "index.py", line 8, in <module> 
    app.config.from_object('config') 
    File "C:\Python27\lib\site-packages\flask\config.py", line 162, in from_object 
    obj = import_string(obj) 
    File "C:\Python27\lib\site-packages\werkzeug\utils.py", line 418, in import_string 
    __import__(import_name) 
    File "C:\Users\****\Desktop\*****\config.py", line 1, in <module> 
    from index import app 
    File "C:\Users\***\Desktop\*****\index.py", line 17, in <module> 
    @app.route('/') 
    File "C:\Python27\lib\site-packages\werkzeug\local.py", line 343, in __getattr__ 
    return getattr(self._get_current_object(), name) 
    File "C:\Python27\lib\site-packages\werkzeug\local.py", line 302, in _get_current_object 
    return self.__local() 
    File "C:\Python27\lib\site-packages\flask\globals.py", line 34, in _find_app 
    raise RuntimeError('working outside of application context') 
RuntimeError: working outside of application context 

注 - エラーが示唆するように、config.pyの162行目にコードはありません。

答えて

0

Application ContextsのFlaskの説明を参照してください。あなたのconfig.pyファイルで

、要求が(それゆえエラー)になるまで、デフォルトでは何のアプリケーションコンテキストはありませんけれどもapp.config['PROPERTY_1'] = 'configgoeshere'への呼び出しは、実際に、current_appに設定を設定しようとするようfrom flask import current_app as appがそれを作ります。その呼び出しは関数内にないので、要求のような何かが起こる直前に実行されます。

current_appではなく、インデックスのappインスタンスで設定を行うことをお勧めします。

+0

私は本当にそれを別のファイルに保存したいのですが、それは私のメインファイルに余分な行を追加するためです。アプリケーションコンテキストのインデックスから設定ファイルに "渡す"方法がありますか?私は__ init __.pyやフラスコの青写真のようなソリューションを見てきましたが、彼らは過剰なもののように見えます – 24x7

+0

'config.py'では' current_app'の代わりに 'index'から' app'をインポートしてconfigを設定することができました。 – Karin

+0

'app.config.from_object( 'config')'をindex.pyに追加し、 'index import app'をconfig.pyに追加しましたが、' RuntimeError:アプリケーションコンテキスト外で働く 'というエラーメッセージが表示されています。何か案は? – 24x7

関連する問題