一、Flask簡介
Flask 是一個 Python 實現的 Web 開發微框架。官網:http://flask.pocoo.org/
二、Demo
1、代碼結構
.
├── 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、主程序blog.py
#!/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'請先登錄'
??? islogin = session.get('islogin')
??? nav_list = [u'首頁',u'經濟',u'文化',u'科技',u'娛樂']
??? blog = {'title':'welcome to my blog','content':'hello, welcome to my blog.'}
??? blogtag = {'javascript':10,"python":20,"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, islogin=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)
主要有首頁、注冊、登錄、上傳頁面。
blog.py主要是展示了Flask中常見功能用法:路由,數據庫操作,cookie,session,redirect,表單,文件上傳,調試,Web服務器的IP和端口,靜態文件讀取等。
3、首頁模板index.html
???
???
???
???
??????? {%if islogin == '1' %}
???????
Welcome ,{{username}}!
??????? {%else%}
???????
{{username}}!
??????? {%endif%}
???????
???
???
???????
???????????
{{blog['title']}}
???????????
???????????????

???????????????
{{blog['content']}}
???????????????
???????????
???????
???????
???????????
- {{key}}({{value}})
??????????????? {%for key,value in blogtag.items()%}
???????????????????
??????????????? {%endfor%}
???????????
???????
???
這個模板主要展示了在Flask模板中如何讀取各種類型的變量。
4、登錄頁面login.html
???
???
???
???
???????
Login
???
???
???????
???????????
???????
???
結合blog.py主要展示表單如何提交取值,cookie和session應用。
5、注冊頁面regist.html
???
???
???
???
???????
Regist
???
???
???????
???????????
???????
???
結合blog.py主要展示了數據庫操作。
6、上傳頁面upload.html
???
???
???
???
???????
Upload
???
???
???????
???????????
???????
???
結合blog.py主要展示了如何上傳文件。
7、運行效果
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
