I. Introduction of FLASK
Flask is a Python-implemented WEB development micro-framework. Official website: http://flask.pocoo.org/
Second, Demo
1. Code structure
Copy the Code code as follows:
.
├──blog.py
├──static
│├──css
││└──index.css
│├──images
││├──cat.jpg
││└──sheying1229.jpg
│└──js
└──templates
├──index.html
├──login.html
├──regist.html
└──upload.html
5 directories, 8 files
2, the main program blog.py
Copy the Code code as follows:
#!/usr/bin/python
#coding: UTF8
From flask import flask, render_template, url_for, request,redirect,make_response,session
Import Os,mysqldb
App = Flask (__name__)
app.secret_key= ' AFJLSJFOWFLAJFLKAJFKJFKALJF '
User_list = [' Jim ', ' Max ', ' py ']
ImagePath = Os.path.join (OS.GETCWD (), "Static/images")
@app. Route ('/')
Def index ():
Username = request.cookies.get (' username ')
if not username:
Username = U ' please login first '
IsLogin = session.get (' islogin ')
Nav_list = [u ' home ', u ' economy ', u ' culture ', U ' technology ', U ' entertainment ']
Blog = {' title ': ' Welcome to my blog ', ' content ': ' Hello, Welcome to my blog. '
Blogtag = {' JavaScript ': ten, ' Python ': "Shell": 5}
img = url_for (' Static ', filename= "images/cat.jpg")
Return render_template (' index.html ', nav_list=nav_list, username=username, blog = blog, Blogtag = Blogtag, img=img, Islog In=islogin)
@app. Route ('/reg ', methods=[' GET ', ' POST ')
def regist ():
if Request.method = = ' POST ':
Username = request.form[' username ']
conn = MySQLdb.connect (user= ' root ', passwd= ' admin ', host= ' 127.0.0.1 ')
conn.select_db (' blog ')
Curr = Conn.cursor ()
sql = ' INSERT INTO ' user ' (' id ', ' username ') values (%d, '%s ') '% (1,username)
Curr.execute (SQL)
Conn.commit ()
Curr.close ()
Conn.close ()
Return "User%s regist ok!"% request.form[' username ']
Else
#request. args[' username ']
Return render_template (' regist.html ')
@app. Route ('/upload ', methods=[' GET ', ' POST ')
def upload ():
if Request.method = = ' POST ':
Username = request.form[' username ']
FILE = Request.files[' img ']
filename = File.filename
File.save (Os.path.join (imagepath,filename))
Return ""% filename
Else
Return render_template (' upload.html ')
@app. Route ('/login/', methods=[' GET ', ' POST ')
def login ():
if Request.method = = ' POST ':
Username = request.form.get (' username ')
If username in user_list:
Response = Make_response (redirect ('/'))
Response.set_cookie (' username ', value=username, max_age=300)
session[' islogin ' = ' 1 '
return response
Else
session[' islogin ' = ' 0 '
Return redirect ('/login/')
Else
Return render_template (' login.html ')
if __name__ = = ' __main__ ':
App.run (debug=true,host= ' 0.0.0.0 ', port=5000)
Main home, registration, login, upload page.
blog.py mainly shows the usage of common functions in flask: routing, database operation, Cookie,session,redirect, forms, file uploading, debugging, Web server IP and port, static file reading, etc.
3. Homepage template index.html
Copy the Code code as follows:
<title>Flask DEMO</title>
{%if IsLogin = = ' 1 '%}
Welcome, {{username}}!
{%else%}
{{username}}!
{%endif%}
{%for nav in nav_list%}
- {{NAV}}
{%endfor%}
{{blog[' title '}}
{{blog[' content '}}
{%for key,value in Blogtag.items ()%}
- {{Key}} ({{value}})
{%endfor%}
This template mainly shows how to read various types of variables in the flask template.
4. Login Page login.html
Copy the Code code as follows:
<title>Login</title>
Login
Combine blog.py main Show form how to submit values, cookies and session applications.
5. Registration Page regist.html
Copy the Code code as follows:
<title>Regist</title>
Regist
The combination of blog.py mainly shows the database operation.
6. Upload page upload.html
Copy the Code code as follows:
<title>Upload</title>
Upload
The combination of blog.py mainly shows how to upload files.
7. Operation Effect