1. Installing and configuring the Python3.6+flask+mysql database
(1) Download and install MySQL database
(2) Download and install Mysql-python middleware
(3) pip install Flask-sqlalchemy (Python ORM Framework SQLAlchemy)
2.mysql Creating a Database
3. Database configuration information config.py
' Mysql+pymysql://root:@127.0.0.1:3306/mis_db?charset=utf8 ' = False
4. Build MySQL and App connection
from Import flask,render_template from Import SQLAlchemy Import = Flask (__name__) app.config.from_object (config) DB=sqlalchemy (APP)
5. Create a user model
fromFlaskImportflask,render_template fromFlask_sqlalchemyImportSQLAlchemyImportConfigapp= Flask (__name__) App.config.from_object (config) DB=SQLAlchemy (APP)classUser (db. Model):__tablename__='User'ID=db. Column (db. Integer,primary_key=true,autoincrement=True) Username= db. Column (db. String (nullable=),False) Password= db. Column (db. String (nullable=),False) Nickname= db. Column (db. String (50) ) Db.create_all () @app. Route ('/login')deflogin ():returnRender_template ('login.html') @app. Route ('/register')defRegister ():returnRender_template ('register.html') @app. Route ('/question')defquestion ():returnRender_template ('question.html') @app. Route ('/base')defBase ():returnRender_template ('base.html') @app. Route ('/')defindex ():returnRender_template ('index.html')if __name__=='__main__': App.run (Debug=true)
Connect to MySQL database and create user model