Initialize the flask environment
pip install pipenv mkdir flasky pipenv shell pipenv install flask # Install plug-ins pipenv install flask-sqlalchemy flask-migrate flask-wtf flask-mail pymysql mkdir app
Create an. env file. The. env file will be loaded when using pipenv
# .env FLASK_APP=run.py FLASK_DEBUG = 1
app initialization
Create a new initialization file and use the factory function to create an app so that when you write unittest later, you can dynamically pass in different configurations to generate an app instance.
# app/__init__.py from flask import Flask from config import config from flask_sqlalchemy import SQLAlchemy from flask_mail import Mail # Database connection db = SQLAlchemy() # Send mail mail = Mail() def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) # Initialize various plug-ins config[config_name].init_app(app) db.init_app(app) mail.init_app(app) return app
Profile initialization
New config.py
# config.py # coding: utf-8 import os class Config(object): SECRET_KEY = os.getenv('SECRET') MAIL_SERVER = os.getenv('MAIL_SERVER') MAIL_PORT = os.getenv("MAIL_PORT") MAIL_USE_TLS = os.getenv("MAIL_USE_TLS") MAIL_USERNAME = os.getenv("MAIL_USERNAME") MAIL_PASSWD = os.getenv("MAIL_PASSWD") SQLALCHEMY_TRACK_MODIFICATIONS = False def init_app(self): pass class DevConfig(Config): DEBUG = True SQLALCHEMY_DATABASE_URI = os.getenv("DEV_SQLALCHEMY_DATABASE_URI") or "mysql+pymysql://root:qwe123@127.0.0.1/flasky" class TestingConfig(Config): Testing = True SQLALCHEMY_DATABASE_URI = os.getenv("TEST_SQLALCHEMY_DATABASE_URI") or "mysql+pymysql://root:qwe123@127.0.0.1/flasky" class ProdConfig(Config): SQLALCHEMY_DATABASE_URI = os.getenv("SQLALCHEMY_DATABASE_URI") or "mysql+pymysql://root:qwe123@127.0.0.1/flasky" config = { "dev":DevConfig, "test":TestingConfig, "prod":ProdConfig, "default":DevConfig }
Add content to. env file
# .env ... SECRET_KEY = 81785a8c-1f9a-4cfb-bc9d-90a8374bbc15 MAIL_SERVER = xxx MAIL_PORT = xxx MAIL_USE_TLS = xx MAIL_USERNAME = tester MAIL_PASSWD = qwe123 DEV_SQLALCHEMY_DATABASE_URI= xxx TEST_SQLALCHEMY_DATABASE_URI=xxx SQLALCHEMY_DATABASE_URI = xxx
Initialize log
When the application is running, the best practice is not to directly output the error information to the page for users to see, which is neither professional nor safe. The best practice is to direct the user to 500 pages when the application has errors, and then log the errors for the development to investigate.
# app/__init__.py from logging.handlers import SMTPHandler,RotatingFileHandler def create_app(config_name): ... if not app.debug: # Record to file if not os.path.exists('logs'): os.mkdir('logs') file_handler = RotatingFileHandler('logs/flasky.log',maxBytes=10240,backupCount=10) file_handler.setFormatter(logging.Formatter( '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')) file_handler.setLevel(logging.INFO) app.logger.addHandler(file_handler) app.logger.info('app start.') # Send mail without setting auth = None if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWD']: auth = (app.config['MAIL_USERNAME'],app.config['MAIL_PASSWD']) secure = None if app.config['MAIL_USE_SSL']: secure = () mail_handler = SMTPHandler( mailhost=app.config['MAIL_SERVER'], fromaddr=app.config['MAIL_USERNAME'], toaddrs=app.config['ADMINS'], subject="flasky Error", credentials=auth,secure=secure) mail_handler.setLevel(logging.ERROR) app.logger.addHandler(mail_handler)
Blueprint / plug and play view
Functional modules are used to divide blueprints, and plug and play views are used to separate routing and logical processing. Easy code management.
Create a new index folder and add the file "init". Py "
# index/__init__.py from flask import Blueprint index_bp = Blueprint("index",__name__) from . import views,routes # index/views.py from flask.views import MethodView from flask import render_template class Index(MethodView): def get(self): return render_template("index.html") # index/routes.py from index import index_bp from index.views import Index index_bp.add_url_rule('/',view_func=Index.as_view('index')) # When you need URL for, you can use the name index.index # url_for('index.index') index can be compared to the name of this function
This completes the basic setup of a blueprint. Next, add the blueprint to the app instance
# app/__init__.py def create_app(config_name): ... from app.index import index_bp app.register_blueprint(index_bp) ... return app
test
Test whether the initialization function is normal
pipenv shell flask run
open http://localhost : 5000 check for errors
Code address: https://github.com/TheFifthMa...
Reference resources
Recommended reading: oreally. Flask. Web. Development. 2nd. Edition