加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python学习笔记十三:Flask demo

发布时间:2020-12-17 00:04:08 所属栏目:Python 来源:网络整理
导读:一、Flask简介 Flask 是一个 Python 实现的 Web 开发微框架。官网: 二、Demo 1、代码结构 span style="color: #800080;"5 directories,span style="color: #800080;"8 files 2、主程序blog.py span style="color: #0000ff;"from flask span style="color: #

一、Flask简介

Flask 是一个 Python 实现的 Web 开发微框架。官网:

二、Demo

1、代码结构

<span style="color: #800080;">5 directories,<span style="color: #800080;">8 files

2、主程序blog.py

<span style="color: #0000ff;">from flask <span style="color: #0000ff;">import<span style="color: #000000;"> Flask,render_template,url_for,request,redirect,make_response,session
<span style="color: #0000ff;">import<span style="color: #000000;"> os,MySQLdb

app = Flask(<span style="color: #800080;">name<span style="color: #000000;">)
app.secret_key=<span style="color: #800000;">'<span style="color: #800000;">afjlsjfowflajflkajfkjfkaljf<span style="color: #800000;">'<span style="color: #000000;">
user_list = [<span style="color: #800000;">'<span style="color: #800000;">jim<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">max<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">py<span style="color: #800000;">'<span style="color: #000000;">]

imagepath = os.path.join(os.getcwd(),<span style="color: #800000;">"<span style="color: #800000;">static/images<span style="color: #800000;">"<span style="color: #000000;">)

@app.route(<span style="color: #800000;">'<span style="color: #800000;">/<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">def<span style="color: #000000;"> index():
username = request.cookies.get(<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">if <span style="color: #0000ff;">not<span style="color: #000000;"> username:
username = u<span style="color: #800000;">'<span style="color: #800000;">请先登录<span style="color: #800000;">'<span style="color: #000000;">
islogin = session.get(<span style="color: #800000;">'<span style="color: #800000;">islogin<span style="color: #800000;">'<span style="color: #000000;">)
nav_list = [u<span style="color: #800000;">'<span style="color: #800000;">首页<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">经济<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">文化<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">科技<span style="color: #800000;">',u<span style="color: #800000;">'<span style="color: #800000;">娱乐<span style="color: #800000;">'<span style="color: #000000;">]
blog = {<span style="color: #800000;">'<span style="color: #800000;">title<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">welcome to my blog<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">content<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">hello,welcome to my blog.<span style="color: #800000;">'<span style="color: #000000;">}
blogtag = {<span style="color: #800000;">'<span style="color: #800000;">javascript<span style="color: #800000;">':10,<span style="color: #800000;">"<span style="color: #800000;">python<span style="color: #800000;">":20,<span style="color: #800000;">"<span style="color: #800000;">shell<span style="color: #800000;">":5<span style="color: #000000;">}
img = url_for(<span style="color: #800000;">'<span style="color: #800000;">static<span style="color: #800000;">',filename=<span style="color: #800000;">"<span style="color: #800000;">images/cat.jpg<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">return render_template(<span style="color: #800000;">'<span style="color: #800000;">index.html<span style="color: #800000;">',nav_list=nav_list,username=username,blog = blog,blogtag = blogtag,img=img,islogin=<span style="color: #000000;">islogin)

@app.route(<span style="color: #800000;">'<span style="color: #800000;">/reg<span style="color: #800000;">',methods=[<span style="color: #800000;">'<span style="color: #800000;">GET<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">])
<span style="color: #0000ff;">def<span style="color: #000000;"> regist():
<span style="color: #0000ff;">if request.method == <span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">:
username = request.form[<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">'<span style="color: #000000;">]
conn = MySQLdb.connect(user=<span style="color: #800000;">'<span style="color: #800000;">root<span style="color: #800000;">',passwd=<span style="color: #800000;">'<span style="color: #800000;">admin<span style="color: #800000;">',host=<span style="color: #800000;">'<span style="color: #800000;">127.0.0.1<span style="color: #800000;">'<span style="color: #000000;">)
conn.select_db(<span style="color: #800000;">'<span style="color: #800000;">blog<span style="color: #800000;">'<span style="color: #000000;">)
curr =<span style="color: #000000;"> conn.cursor()
sql = <span style="color: #800000;">'<span style="color: #800000;">insert into user (id,username) values (%d,"%s")<span style="color: #800000;">' % (1<span style="color: #000000;">,username)
curr.execute(sql)
conn.commit()
curr.close()
conn.close()
<span style="color: #0000ff;">return <span style="color: #800000;">"<span style="color: #800000;">user %s regist ok!<span style="color: #800000;">" % request.form[<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">'<span style="color: #000000;">]
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #008000;">#<span style="color: #008000;">request.args['username']
<span style="color: #0000ff;">return render_template(<span style="color: #800000;">'<span style="color: #800000;">regist.html<span style="color: #800000;">'<span style="color: #000000;">)

@app.route(<span style="color: #800000;">'<span style="color: #800000;">/upload<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">])
<span style="color: #0000ff;">def<span style="color: #000000;"> upload():
<span style="color: #0000ff;">if request.method == <span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">:
username = request.form[<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">'<span style="color: #000000;">]
file = request.files[<span style="color: #800000;">'<span style="color: #800000;">img<span style="color: #800000;">'<span style="color: #000000;">]
filename =<span style="color: #000000;"> file.filename
file.save(os.path.join(imagepath,filename))
<span style="color: #0000ff;">return <span style="color: #800000;">"<span style="color: #800000;"><span style="color: #800000;">" %<span style="color: #000000;"> filename
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">return render_template(<span style="color: #800000;">'<span style="color: #800000;">upload.html<span style="color: #800000;">'<span style="color: #000000;">)

@app.route(<span style="color: #800000;">'<span style="color: #800000;">/login/<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">])
<span style="color: #0000ff;">def<span style="color: #000000;"> login():
<span style="color: #0000ff;">if request.method == <span style="color: #800000;">'<span style="color: #800000;">POST<span style="color: #800000;">'<span style="color: #000000;">:
username = request.form.get(<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">if username <span style="color: #0000ff;">in<span style="color: #000000;"> user_list:
response = make_response(redirect(<span style="color: #800000;">'<span style="color: #800000;">/<span style="color: #800000;">'<span style="color: #000000;">))
response.set_cookie(<span style="color: #800000;">'<span style="color: #800000;">username<span style="color: #800000;">',value=username,max_age=300<span style="color: #000000;">)
session[<span style="color: #800000;">'<span style="color: #800000;">islogin<span style="color: #800000;">'] = <span style="color: #800000;">'<span style="color: #800000;">1<span style="color: #800000;">'
<span style="color: #0000ff;">return<span style="color: #000000;"> response
<span style="color: #0000ff;">else<span style="color: #000000;">:
session[<span style="color: #800000;">'<span style="color: #800000;">islogin<span style="color: #800000;">'] = <span style="color: #800000;">'<span style="color: #800000;">0<span style="color: #800000;">'
<span style="color: #0000ff;">return redirect(<span style="color: #800000;">'<span style="color: #800000;">/login/<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">else<span style="color: #000000;">:
<span style="color: #0000ff;">return render_template(<span style="color: #800000;">'<span style="color: #800000;">login.html<span style="color: #800000;">'<span style="color: #000000;">)

<span style="color: #0000ff;">if <span style="color: #800080;">name == <span style="color: #800000;">'<span style="color: #800000;">main<span style="color: #800000;">'<span style="color: #000000;">:
app.run(debug=True,host=<span style="color: #800000;">'<span style="color: #800000;">0.0.0.0<span style="color: #800000;">',port=5000)

主要有首页、注册、登录、上传页面。

blog.py主要是展示了Flask中常见功能用法:路由,数据库操作,cookie,session,redirect,表单,文件上传,调试,Web服务器的IP和端口,静态文件读取等。

3、首页模板index.html

Flask DEMO Welcome,{{username}}!{{username}}! <{{nav}} {{blog['title']}} {{blog['content']}} {{key}}({{value}})

这个模板主要展示了在Flask模板中如何读取各种类型的变量。

4、登录页面login.html

Login Login <

结合blog.py主要展示表单如何提交取值,cookie和session应用。

5、注册页面regist.html

Regist Regist <

结合blog.py主要展示了数据库操作。

6、上传页面upload.html

Upload Upload < <

结合blog.py主要展示了如何上传文件。

7、运行效果

三、参考资料

1、

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读