0
0
FlaskHow-ToBeginner · 3 min read

How to Use Config from Class in Flask: Simple Guide

In Flask, you can load configuration settings from a class by creating a config class with attributes and then calling app.config.from_object() with that class. This method helps organize settings cleanly and keeps your app configuration modular.
📐

Syntax

To use configuration from a class in Flask, define a class with uppercase attributes for each setting. Then load it into your Flask app using app.config.from_object().

  • class ConfigClass: - Your config class.
  • ATTRIBUTE_NAME = value - Config variables must be uppercase.
  • app.config.from_object(ConfigClass) - Loads config into Flask app.
python
class ConfigClass:
    DEBUG = True
    SECRET_KEY = 'mysecretkey'

from flask import Flask
app = Flask(__name__)
app.config.from_object(ConfigClass)

print(app.config['DEBUG'])  # True
print(app.config['SECRET_KEY'])  # 'mysecretkey'
Output
True mysecretkey
💻

Example

This example shows a complete Flask app using a config class to set debug mode and a secret key. The app prints these config values to confirm they loaded correctly.

python
from flask import Flask

class Config:
    DEBUG = True
    SECRET_KEY = 'supersecret'

app = Flask(__name__)
app.config.from_object(Config)

@app.route('/')
def index():
    debug_status = app.config['DEBUG']
    secret = app.config['SECRET_KEY']
    return f'Debug is {debug_status}, Secret Key is {secret}'

if __name__ == '__main__':
    app.run()
Output
When you visit http://127.0.0.1:5000/ in your browser, it shows: Debug is True, Secret Key is supersecret
⚠️

Common Pitfalls

Common mistakes when using config classes in Flask include:

  • Using lowercase attribute names (Flask only loads uppercase keys).
  • Forgetting to call app.config.from_object() before accessing config.
  • Modifying config class attributes after loading (it won't update the app config).

Always keep config keys uppercase and load config early in your app setup.

python
from flask import Flask

class BadConfig:
    debug = True  # lowercase, won't be loaded

app = Flask(__name__)
app.config.from_object(BadConfig)

print(app.config.get('debug'))  # None, wrong key
print(app.config.get('DEBUG'))  # None, not set

# Correct way:
class GoodConfig:
    DEBUG = True

app.config.from_object(GoodConfig)
print(app.config['DEBUG'])  # True
Output
None None True
📊

Quick Reference

Summary tips for using config classes in Flask:

  • Define config variables as uppercase attributes in a class.
  • Load config with app.config.from_object(YourConfigClass).
  • Access config via app.config['KEY_NAME'].
  • Use config classes to separate settings for different environments.

Key Takeaways

Use uppercase attribute names in your config class for Flask to recognize them.
Load your config class into Flask with app.config.from_object() before using settings.
Access configuration values via app.config dictionary with the exact uppercase keys.
Config classes help keep your app settings organized and easy to manage.
Avoid changing config class attributes after loading; reload config if needed.