I have followed the layout of my Flask project from http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world.
我从http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world上了解了我的Flask项目的布局。
I have the following structure:
我有以下结构:
app/
__init__.py
views.py
forms.py
myFile.py
run.py
config.py
In views.py, forms.py I am able to use
在views.py中,我可以使用forms.py
from config import basedir
However I cannot use that in myFile.py
但是我不能在myFile.py中使用它
I added
import Flask
and when I modify it, the Flask web server restarts, but it doesn't say found changes in app/myFile.py restarting it just restarts.
当我修改它时,Flask Web服务器重新启动,但它没有说app / myFile.py中发现的更改重新启动它只是重新启动。
What do I need to do to be able to use
我需要做什么才能使用
from config import basedir
in my python file. I don't see anything special being done in __init__.py
for forms.py.
在我的python文件中。我没有看到在__init__.py中为forms.py做了什么特别的事情。
EDIT: This is my __init__.py
file:
编辑:这是我的__init__.py文件:
from flask import Flask
from config import basedir
app = Flask(__name__)
app.config.from_object('config')
from app import views
2 个解决方案
#1
41
When people talk about configs in Flask, they are generally talking about loading values into the app's configuration. In your above example you could have something like app.config.from_object('config')
in your init.py
file. Then all the configuration values will be loaded into the app.config
dictionary.
当人们谈论Flask中的配置时,他们通常都在谈论将值加载到应用程序的配置中。在上面的示例中,您可以在init.py文件中使用app.config.from_object('config')。然后,所有配置值都将加载到app.config字典中。
Then in any of your files you could just import the app object to gain access to that dictionary. I tend to access that app
object by doing from flask import current_app as app
then just app.config['MY_SETTING']
to get the value I care about. Read up more in the documenation.
然后在您的任何文件中,您只需导入应用程序对象即可访问该字典。我倾向于通过使用flask import current_app作为app然后只是app.config ['MY_SETTING']来访问该app对象,以获得我关心的值。阅读文档中的更多内容。
#2
13
After a little bit of fiddling (and a little help from the 'net), I could improve this further, by changing the code to include the config to:
经过一点点摆弄(以及来自'net的一点帮助)后,我可以通过更改代码以包含配置来进一步改进这一点:
app.config.from_object('config.ProductionConfig')
This enables this cool pattern for configurations:
这为配置提供了这种酷炫的模式:
class Config(object):
DEBUG = True
DEVELOPMENT = True
SECRET_KEY = 'do-i-really-need-this'
FLASK_HTPASSWD_PATH = '/secret/.htpasswd'
FLASK_SECRET = SECRET_KEY
DB_HOST = 'database' # a docker link
class ProductionConfig(Config):
DEVELOPMENT = False
DEBUG = False
DB_HOST = 'my.production.database' # not a docker link
What's left now is to see how to integrate testing configs into this, but at least it feels less clumsy.
现在剩下的就是看看如何将测试配置集成到这里,但至少它感觉不那么笨拙。
#1
41
When people talk about configs in Flask, they are generally talking about loading values into the app's configuration. In your above example you could have something like app.config.from_object('config')
in your init.py
file. Then all the configuration values will be loaded into the app.config
dictionary.
当人们谈论Flask中的配置时,他们通常都在谈论将值加载到应用程序的配置中。在上面的示例中,您可以在init.py文件中使用app.config.from_object('config')。然后,所有配置值都将加载到app.config字典中。
Then in any of your files you could just import the app object to gain access to that dictionary. I tend to access that app
object by doing from flask import current_app as app
then just app.config['MY_SETTING']
to get the value I care about. Read up more in the documenation.
然后在您的任何文件中,您只需导入应用程序对象即可访问该字典。我倾向于通过使用flask import current_app作为app然后只是app.config ['MY_SETTING']来访问该app对象,以获得我关心的值。阅读文档中的更多内容。
#2
13
After a little bit of fiddling (and a little help from the 'net), I could improve this further, by changing the code to include the config to:
经过一点点摆弄(以及来自'net的一点帮助)后,我可以通过更改代码以包含配置来进一步改进这一点:
app.config.from_object('config.ProductionConfig')
This enables this cool pattern for configurations:
这为配置提供了这种酷炫的模式:
class Config(object):
DEBUG = True
DEVELOPMENT = True
SECRET_KEY = 'do-i-really-need-this'
FLASK_HTPASSWD_PATH = '/secret/.htpasswd'
FLASK_SECRET = SECRET_KEY
DB_HOST = 'database' # a docker link
class ProductionConfig(Config):
DEVELOPMENT = False
DEBUG = False
DB_HOST = 'my.production.database' # not a docker link
What's left now is to see how to integrate testing configs into this, but at least it feels less clumsy.
现在剩下的就是看看如何将测试配置集成到这里,但至少它感觉不那么笨拙。