The first Python web development.
Related to: Flask architecture, MySQL database.
Function: Diary. Development.
Flask architecture, MySQL database.
Function: Diary.
1 fromFlaskImportflask,render_template2 fromFlaskImportRequest3 ImportPymysql4 5 6 7 #Example flask This class8App = Flask (__name__)9 Ten # One@app. Route ('/') A defIndex (): - returnRender_template ('index.html') - the@app. Route ('/creat') - defcreatdiary (): - returnRender_template ('creat.html') - + - +conn =Pymysql.connect ( Ahost='127.0.0.1', atUser='Root', -password='USBW', -port=3307, -Database='Diary', -charset='UTF8' - ) incursor = Conn.cursor (cursor=pymysql.cursors.DictCursor) - to@app. Route ('/upload', methods=['GET','POST'])#The only way to ask for it is get + defupload (): -Date=request.form.get ('Date') theKey=request.form.get ('Key') *Title=request.form.get ('title') $Content=request.form.get ('content')Panax Notoginseng #files=request.files.get (' file ') - Print(date,key,title,content) theSql="INSERT into ' Day ' (' Date ', ' class ', ' title ', ' content ') VALUES (%s,%s,%s,%s)" + Cursor.execute (sql,[date,key,title,content]) A Conn.commit () the #cursor.close () + conn.close () - return 'Submit Success! ' $ $@app. Route ('/diarylist') - defdiarylist (): - returnRender_template ('diarylist.html') the - if __name__=='__main__':WuyiApp.run ()
Python Web Development 1