Python Flask message board (no database)

A py file, an html file, can be run directly

py file

 1 from flask import Flask, request, render_template, redirect, url_for
 2 import time
 3 
 4 app = Flask(__name__)
 5 
 6 users = []  # 这里存放所有的留言
 7 
 8 @app.route('/say/', methods=['GET', 'POST'])
 9 def index():
10     if request.method == 'GET':
11         return render_template('index.html', says=users)
12 
13     else:
14         title = request.form.get('say_title')
15         text = request.form.get('say')
16         user = request.form.get('say_user')
17         date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
18 
19         users.append({"title": title,
20                       "text": text,
21                       "user": user,
22                       "date": date})
23         return redirect(url_for('index'))
24 
25 if __name__ == '__main__':
26     app.run(debug=True, )

index.html:

 1 <!DOCTYPE html>
 2 <html lang="zh">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>留言板</title>
 6 </head>
 7 <body>
 8         <center><h1>留言</h1></center>
 9         <center>
10             <form action="{{ url_for('index') }}" method="POST">
11                 用户名: <input type="text" name="say_user"><br>
12                 留言标题: <input type="text" name="say_title"><br>
13                 留言内容: <input type="text" name="say"><br>
14                 <button</Submit message>button>
15             </form>
16         </center>
17     <center>
18         <table border="2">
19             <thead>
20             <tr>
21                 <th>标题</th>
22                 <th>内容</th>
23                 <th>用户</th>
24                 <th></Message timeth>
25             </tr>
26             </thead>
27             <tbody>
28             {% for say in says %}
29                 <tr>
30                     <td>{{ say['title'] }}</td>
31                     <td>{{ say['text'] }}</td>
32                     <td>{{ say['user'] }}</td>
33                     <td>{{ say['date'] }}</td>
34                 </tr>
35             {% endfor %}
36             </tbody>
37         </table>
38     </center>
39 </body>
40 </html>

 result:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324834114&siteId=291194637