One, the realization function: Generates the login interface through the Web form, when the input user name is not in the background database, adds in, when, displays happy see you again style;
1. Database Engine: MySQL (install no path required, I was not moved before)
2. Database framework: Flask-sqlalchemy (book recommended, direct pip install Flask-sqlalchemy on line)
Second, Flask-sqlalchemy to build a simple table:
aa.py:
#Coding:utf-8 fromFlaskImportFlask fromFlask_scriptImportManager fromFlask_sqlalchemyImportSqlalchemyapp= Flask (__name__) app.config['Sqlalchemy_database_uri'] ='Mysql://[email protected]/yxctest' #MySQL database uilmysql://username:[email protected]/databaseapp.config['Sqlalchemy_commit_on_teardown'] = True#Real-time updatesDB= SQLAlchemy (APP)#Instantiate DBManager =Manager (APP)classUser (db. Model):__tablename__='Users' #Table nameID = 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.usernameif __name__=='__main__': Manager.run ()
Then enter the Python aa.py shell in doc
The table can then be manipulated:
From AA import db, from AA import User, Db.create_all (), user1 = User (username = ' Yxc ', email= ' [email protected] ', password= ' 1 23123 ')
The above is the DOC directive, function is import, build table, insert row, etc.;
Third, the realization of the function in step one:
Quite simply, merge the code for this database with the previous section of the Web form:
bb.py:
#Coding:utf-8Importsysreload (SYS) sys.setdefaultencoding ('Utf-8') fromFlaskImportFlask, Render_template,url_for,session,redirect,flash fromFlask_scriptImportManager fromFlask_bootstrapImportBootstrap fromFlask_momentImportmoment fromFlask_wtfImportForm fromWtformsImportStringfield, Submitfield fromWtforms.validatorsImportRequired fromFlask_sqlalchemyImportSQLAlchemy#Import Frame hereapp= Flask (__name__) app.config['Sqlalchemy_database_uri'] ='Mysql://[email protected]/yxctest' #own MySQL account and build the tableapp.config['Sqlalchemy_commit_on_teardown'] =True#app.config[' sqlalchemy_track_modifications ') = Truedb = SQLAlchemy (APP)#DB InstanceBootstrap =Bootstrap (APP) App.secret_key='1234567'classNameform (Form):#several fragments defined in a Web tableName = Stringfield ('What is your name?', validators=[Required ()]) email= Stringfield ('What is your email', validators=[Required ()]) password= Stringfield ('What is your password', validators=[Required ()]) Submit= Submitfield ('Submit')classUser (db. Model):#table of the defined database __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.username@app.route ('/', methods=['GET','POST'])defindex (): Form=Nameform ()ifForm.validate_on_submit:user= User.query.filter_by (username = form.name.data). First ()#query statement, exactly query the name of the input and the name in the database table ifUser isNone:user= User (username = form.name.data,email=form.email.data,password=form.password.data)#Insert a row if it does not existdb.session.add (user) Db.session.commit ()#these two lines are necessary.session['known'] =FalseElse: session['known'] =True session['name']=Form.name.data Form.name.data="' returnRender_template ('index.html', Form=form,name=session.get ('name'), known = Session.get ('known', False))if __name__=='__main__': App.run (Debug=true)
Of course, our template rendering also has to be changed:
Index.html:
{% extends"bootstrap/base.html"%}{%Import "bootstrap/wtf.html"As WTF%}{% block Title%}ttttt{% Endblock%}{% Block content%}<divclass="Container"><divclass="Page-header">ifName%}{{name}}{%Else%}stranger{% endif%}!{%if notknown%}<p>please Meet you</p>{%Else%}<p>happy See you again</p>{%endif%}</div></div>{{wtf.quick_form (form)}}{% Endblock%}
Effect:
"Flask Web Development" Note 4: Database