One: __init__.py effect:
The purpose of the __init__.py file is to change the folder into a Python module , with a__init__.py file in each of the modules in the Python package. Normally the __init__.py file is empty, but we can add additional functionality to it. when we imported a package, we actually imported its __init__.py file . This allows us to bulk import the modules we need in the __init__.py file, instead of needing one import.
__init__.py:
from Import Flask, url_for, request, redirect, Render_template from Import = Flask (__name__) app.config.from_object ('config') = SQLAlchemy (APP) fromimport models,views
models.py
__TABLENAME__: Table name.
In __repr__:python, this _repr_ function, which corresponds to the Repr (object) function, returns a printable string that can be used to represent an object. You can print out objects
fromAppImportDb#DB is an associated SQLAlchemy instance that is generated in app/__init__.pyclassUser (db. Model):__tablename__='Users'ID= db. Column (db. Integer, primary_key=True) Username= db. Column (db. String (unique=),True) Email= db. Column (db. String (unique=),True) Password= db. Column (db. String (+), nullable=False)def __repr__(self):return '<user%r>'%Self.usernameclassAdmin (db. Model):__tablename__='Admins'ID= db. Column (db. Integer, primary_key=True) Username= db. Column (db. String (unique=),True) Email= db. Column (db. String (unique=),True) Password= db. Column (db. String (+), nullable=False)def __repr__(self):return '<user%r>'% Self.username
create_db.py
Create_all: This function will find the tables, then connect the DB, then create the table.
from Import Dbdb.create_all ()
Execute Python create_db.py with the command line; or directly in Pycharm, right-create_db.py will create the table.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
Three flask+ MySQL database operation