一、Highlight.js网页代码高亮
项目关于页面使用了highlight.js来让代码高亮
地址:http://vd.yzyyz.top/team
二、Python实现的简单留言功能
地址:http://vd.yzyyz.top/inmsg
项目源码:源码:https://github.com/yzyyz1387/flask-visualData-practice
在原先基础上增加了留言页面,原理较为简单,也没互斥锁自旋锁等,基于Flask框架,从网页传递表单数据,存入数据库,再显示出来,源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| import ... import sqlite3 import os import time
...
@app.route("/msg",methods=['POST','GET']) def msg(): if request.method == 'POST': msg = request.form saveMsg(msg,'msg.db') smsg=showmsg() return render_template("test1.html",smsg=smsg) else: smsg=showmsg() return render_template("test1.html",smsg=smsg)
@app.route('/inmsg') def inputmsg(): smsg=showmsg() return render_template('test1.html',smsg=smsg)
def createtable(path): sql = ''' create table msgs ( id integer primary key autoincrement, username text, email text, msg text, msgtime text ) ''' conn = sqlite3.connect(path) cur = conn.cursor() cur.execute(sql) conn.commit() conn.close()
def saveMsg(msglist,dbpath): now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) conn = sqlite3.connect(dbpath) cur=conn.cursor() data=[] for ii in msglist.items(): data.append(ii[1]) data.append(now) for i in range(len(data)): data[i]='"'+data[i]+'"' sql=''' insert into msgs (username,email,msg,msgtime) values(%s) '''%",".join(data) cur.execute(sql) conn.commit() conn.close()
def showmsg(): if os.path.exists('msg.db') == True: datalist = [] conn = sqlite3.connect("msg.db") cur = conn.cursor() sql = ''' select * from msgs ''' data = cur.execute(sql) for item in data: datalist.append(item) conn.commit() cur.close() conn.commit() conn.close() smsgdata=datalist return smsgdata else: createtable('msg.db') datalist = [] conn = sqlite3.connect("msg.db") cur = conn.cursor() sql = ''' select * from msgs ''' data = cur.execute(sql) for item in data: datalist.append(item) conn.commit() cur.close() conn.commit() conn.close() smsgdata = datalist return smsgdata
|
html显示留言部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| {% for v in smsg %} <div class="col-md-6 mb-4" data-aos="fade-up" data-aos-delay=""> <div class="service d-flex h-100"> <div class="svg-icon"> <p>{{ v[0] }}楼</p> </div> <div class="service-about"> <h3>{{ v[1] }}</h3> <p>{{ v[3] }}</p> <p style="font-size: 10px">留言于:{{ v[4] }}</p> </div> </div> </div> {% endfor %}
|
html表单部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #表单任意内容不能为空 <script type="text/javascript"> function myCheck() { for (var i = 0; i < document.msgform.elements.length - 1; i++) { if (document.msgform.elements[i].value == "") { alert("留言信息不能有空项哦~~"); document.msgform.elements[i].focus(); return false; } } return true;
} </script>
`
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| <form method="post" action="{{ url_for('msg') }}" class="msgform" name="msgform" onSubmit="return myCheck()"> <div style="margin: 25px" class="form-group"> <input type="text" name="username" placeholder="输入昵称" minlength="2" maxlength="10" class="form-control" id="username" style="margin: 10px;"/> <input type="email" name="email" placeholder="输入邮箱" class="form-control" id="email" style="margin: 10px;"/> <input type="text" name="messg" placeholder="说点什么吧~~" minlength="5" maxlength="100" class="form-control" id="messg" style="margin: 10px;height: 50px"/> <div style="text-align: center"> <input class="btn btn-primary" type="submit" value="提交" onclick="isEmpty()"/> </div> </div>
</form>
|