<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>留言板</title>
<link rel="stylesheet" href="main.css" type="text/css">
</head>
<body>
<h1>留言板</h1>
<form action="/post" method="post">
<p>请留言</p>
<table>
<tr>
<th>名字</th>
<td>
<input type="text" size="20" name="name">
</td>
</td>
</tr>
<tr>
<th>留言</th>
<td>
<textarea rows="5" cols="40" name="comment"></textarea>
</td>
</tr>
</table>
<p><button type="submit">提交</button></p>
</form>
<div class="entries-area">
<h2>留言记录</h2>
<h3>游客 的留言 (2017年11月2日21:45:05)</h3>
<p>
留言内容<br>
留言内容 </p> </div> </body>
</html>
body{
margin:;
padding:;
color: #000E41;
background-color: #004080;
}
h1{
padding: 0 1em;
color: #FFFFFF;
}
form{
padding: 0.5em 2em;
background-color: #78B8F8;
}
.main {
padding:;
}
.entries-area{
padding: 0.5em 2em;
background-color: #FFFFFF;
}
.entries-area p{
padding: 0.5em 1em;
background-color: #DBDBFF;
}
留言板
留言板
请留言
名字 | |
---|---|
留言 |
提交
留言记录
游客 的留言 (2017年11月2日21:45:05)
留言内容
# -*- coding: utf-8 -*-
from __future__ import with_statement
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
from contextlib import closing # configuration
DATABASE = 'D:\coding\py2.7\liuyan\guestbook.dat' # 数据库存储路径
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default' # create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True) def connect_db(): # 快速连接到指定数据库的方法
return sqlite3.connect(app.config['DATABASE']) def init_db(): # 初始化数据库
with closing(connect_db()) as db:
with app.open_resource('schema.sql') as f:
db.cursor().executescript(f.read())
db.commit() @app.before_request
def before_request():
g.db = connect_db() @app.teardown_request
def teardown_request(exception):
g.db.close() @app.route('/')
def show_entries(): # 输出函数,会将条目作为字典传递给 show_entries.html 模板,并返回之后的渲染结果
cur = g.db.execute('select name,email,text from entries order by id desc limit 10')
entries = [dict(name=row[0], email=row[1], text=row[2]) for row in cur.fetchall()]
return render_template('show_entries.html', entries=entries) @app.route('/add', methods=['POST'])
def add_entry(): # 用户添加新的留言信息函数,并只响应 POST 请求,表单显示在 show_entries
if not session.get('logged_in'):
abort(401)
if len(request.form['text']) > 50 and len(request.form['text']) < 500: # 实现控制字数在50到500范围内
g.db.execute('insert into entries (name,email,text) values (?,?,?)',
[request.form['name'], request.form['email'], request.form['text']])
g.db.commit()
flash('New entry was successfully posted')
else:
flash('The input range must be between 50 and 500 characters ') # 如果留言信息不在范围内作出提示
return redirect(url_for('show_entries')) @app.route('/login', methods=['GET', 'POST'])
def login(): # 登入函数
error = None
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME']:
error = 'name error'
elif request.form['password'] != app.config['PASSWORD']:
error = 'password error'
else:
session['logged_in'] = True
flash('log in')
return redirect(url_for('show_entries'))
return render_template('login.html', error=error) @app.route('/logout')
def logout(): # 退出登录函数
session.pop('logged_in', None)
flash('log out')
return redirect(url_for('show_entries')) if __name__ == '__main__':
init_db()
app.run(debug=True)
留言内容