前言

简单的用txt来记录留言,使用List这种占内存的方式。

目录结构

.
├── comm.txt //评论文件
├── main.py //主程序
└── templates
    └── index.html //网页模板

main.py

 #coding=utf-8
 from flask import Flask,render_template,request
 import json,time
 app = Flask(__name__)
 comm = []
 def loadcomm():
    f = open('comm.txt','r')
    buf = f.readline()
    while buf:
            comm.append(json.loads(buf))
            buf = f.readline()
    f.close()
    comm.reverse() //倒序
 def addcomm(name,date,email,content):
    f = open('comm.txt','a+')
    t = {}
    t['name'] = name
    t['date'] = date
    t['email'] = email
    t['content'] = content
    comm.append(t)
    f.write(json.dumps(t))
    f.write('\n')
    f.close()
 @app.route('/',methods=['GET','POST'])
 def hello_world():
    if request.method == 'POST':
            if request.form['name'] and request.form['email'] and request.form['content']:
                    addcomm(request.form['name'],time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),request.form['email'],request.form['content'])
                    return render_template('index.html',error='',text=comm)
            else:
                    return render_template('index.html',error=u'请填写完全!',text=comm)
    else:
            return render_template('index.html',error='',text=comm)
 if __name__ == '__main__':
    loadcomm()
    app.run(host='0.0.0.0',port=80)

index.html

 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8" name="viewport" content="width=device-width,initial-scale=1" />
 <title>留言板</title>
 <link href="https://cdn.bootcss.com/normalize/8.0.1/normalize.css" rel="stylesheet">
 <style>
 html,body{
    padding:0px;
    margin:0px;
    height:100%;
    background-color:#F1F1F1;
    bottom:0;
 }
 .main{
    margin:auto;
    background-color:#FFF;
    padding:6px 16px 6px 16px;
 }
 .logo{
    text-align:center;
 }
 .menu{
    text-align:center;
 }
 .laa{
    line-height:16px;
    padding:6px 12px 6px 12px;
 }
 .footer{
    text-align:center;
    font-size:12px;
    color:#999;
    width:100%;
    margin:auto;
    padding-bottom:12px;
 }
 .button{
    background-color:#4CAF50;
    border:none;
    color:#FFF;
    padding:8px 16px;
    text-align:center;
    text-decoration:none;
    display:inline-block;
    font-size:16px;
    cursor:pointer;
    border-radius:6px;
    margin:2px 6px 2px 6px;
 }
 .button:hover {
     background-color: #3e8e41;
 }
 a{
    text-decoration:none;
    color:#666;
 }
 a:hover{
    color:#999;
 }
 @media screen and (min-width:992px){
    .main{
            width:50%;
    }
 }
 .input,input{
    width:98%;
    border: 1px solid #ccc;
    padding: 6px 0px;
    border-radius: 3px;
    padding-left:6px;
    -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
    box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
    -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
    -o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s
 }
 .input:focus,input:focus{
    border-color: #66afe9;
    outline: 0;
    -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
    box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6)
 }
 .error{
    border-left:6px solid #FF0;
    background-color:#F1F1F1;
    padding:2px 6px 2px 6px;
    line-height:12px;
    font-size:14px;
 }
 </style>
 </head>

 <body>
 <div class="main">
    <div class="logo">
      <h3>留言板</h3>
    </div>
    <div class="start">
            <!-- 错误提示区 -->
         {% if error %}
            <div class="error">
            <p>错误: {{ error }} </p>
            </div>
         {% endif %}
            <!-- 错误提示区结束 -->
            <form name="form1" method="post" action="/">
              <p>
                <label for="name">昵称:</label>
                <input type="text" name="name" id="name">
           </p>
           <p>
                <label for="email">邮箱:</label>
           </p>
              <p>
                <input type="text" name="email" id="email">
          内容:</p>
              <p>
             <textarea name="content" rows="3" class="input" id="content"></textarea>
              </p>
              <p>
                <input type="submit" name="button" id="button" class="button" value="发送">
              </p>
            </form>
    </div>
     <!-- 评论加载区 -->
     {% for buf in text %}
    <div class="laa">
        <h4> {{ buf["name"] }} <small> {{ buf["date"] }} </small></h4>
             <p> {{ buf["content"] }} </p>
    </div>
     {% endfor %}
     <!-- 评论加载区结束 -->
 </div>
 <div class="footer">
    <p>CTIN © 2019</p>
 </div>
 </body>
 </html>

效果

37_0.PNG

文章版权:若叶子 Blog - 人的一生当然要靠自我奋斗,当然也要考虑历史的行程

本文链接:https://chutian.bid/archives/37.html

转载请注明文章原始出处 !

添加新评论

返回顶部
Typecho 强力驱动. © Powered by Zhangtinghui. Theme by Postbird.