<!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)

留言内容

python完成留言板功能的更多相关文章

  1. jsp中运用application实现共享留言板功能

    jsp中application的知识点总结: 1.一个Web应用程序启动后,将会自动创建一个application对象,在整个应用程序的运行过程中只有这一个application对象,即所有访问该网站 ...

  2. Web开发从零单排之二:在自制电子请帖中添加留言板功能,SAE+PHP+MySql

    在上一篇博客中介绍怎样在SAE平台搭建一个html5的电子请帖网站,收到很多反馈,也有很多人送上婚礼的祝福,十分感谢! web开发从零学起,记录自己学习过程,各种前端大神们可以绕道不要围观啦 大婚将至 ...

  3. 利用反馈字段给帝国cms添加留言板功能(图文教程)

    帝国cms的插件中提供信息反馈字段,很多人却不会用.这里谢寒教大家如何来给自己的帝国cms网站添加留言板功能 1.找到添加地址 2.添加字段 3.你可以在字段中添加多种字段类型(有文本域,单行文本框, ...

  4. php实现留言板功能

    这个小小的留言板功能适合班级内或者公司内部之间的讨论,对话和留言,非常的方便,更重要的是无需网络,对于公司管理层来说是非常乐于常见的, 下面是这个留言板的写法: 1 首先是登录页面: <form ...

  5. 使用PHP连接数据库实现留言板功能

    PHP实现留言板功能: 1 首先是登录页面: <!DOCTYPE html><html>    <head>        <meta charset=&qu ...

  6. JS原生编写实现留言板功能

    实现这个留言板功能比较简单,所以先上效果图: 实现用户留言内容,留言具体时间. <script> window.onload = function(){ var oMessageBox = ...

  7. 原生JS实现简单留言板功能

    原生JS实现简单留言板功能,实现技术:css flex,原生JS. 因为主要是为了练手js,所以其中布局上的一些细节并未做处理. <!DOCTYPE html> <html lang ...

  8. 用php(session)实现留言板功能----2017-05-09

    要实现留言功能,发送者和接受者必不可少,其次就是留言时间留言内容. 要实现的功能: 1.登录者只能查看自己和所有人的信息,并能够给好友留言 2.留言板页面,好友采取下拉列表,当留言信息为空时,显示提示 ...

  9. Django web框架开发基础-django实现留言板功能

    1.创建项目 cmd  django-admin startpoject cloudms 2.创建APP cmd django-admin startapp msgapp 3.修改settings,T ...

随机推荐

  1. 前端css之文本操作及块级元素和行内元素

    1.文本操作 1.1文本颜色(color) 颜色指定方式: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0,0) 颜色的名称 - 如:  red 1.2水平对齐方式 ...

  2. Mybatis查询报错:There is no getter for property named '*' in 'class java.lang.String

    问题: 执行查询时报错:There is no getter for property named '*' in 'class java.lang.String 原因: 传过去的参数为识别.本例为 p ...

  3. 时间戳与QDateTime相互转换

    最近项目中需要将日期时间输出到Excel中,程序使用Qt开发,使用第三方库QtXlsx进行Excel读写操作.Excel中第一列为时间,时间间隔为1小时,如图所示. 赋值起始时间stDTime,则后续 ...

  4. 提高篇(1):RMQ问题与ST表

    RMQ是英文Range Minimum/Maximum Query的缩写,是询问某个区间内的最值,这里讲一种解法:ST算法 ST算法通常用在要多次(10^6级别)询问区间最值的问题中,相比于线段树,它 ...

  5. 利用python在windows环境下爬取赶集网工作信息。

    主要用到了多进程和多线程的知识,最后结果保存成csv文件格式,如有需要可改成数据库版本. 对用到的库做下简要介绍,具体请参考官方文档: xpinyin.Pinyin:将输入的中文转成拼音 concur ...

  6. 8-1 python 接口开发(提供数据、返回session_id)

    1.接口开发,根据不同查询条件返回数据库查询结果 import flask import tools import json server = flask.Flask(__name__) #新建一个服 ...

  7. Ado访问sqlserver 端口号非1433时 连接串的写法

    Provider=SQLOLEDB.;Persist Security Info=False;Data Source=hostName,Port //注意用 逗号分隔主机名与端口号

  8. 虚拟机桥接模式下多台Ubuntu16.04系统互相连接

    1.首先新建一个虚拟机并在该虚拟机上安装Ubuntu16.04系统.为这台虚拟机起名为Ubuntu3. 2.对Ubuntu3进行克隆,为新克隆生成的虚拟机起名为Ubuntu2.(这时我们会发现Ubun ...

  9. Linux设置下载站点

    https://blog.csdn.net/jfhkd2012/article/details/50912757

  10. 机器学习笔记(一)—— 线性回归问题与Matlab求解

    给你多组数据集,例如给你很多房子的面积.房子距离市中心的距离.房子的价格,然后再给你一组面积. 距离,让你预测房价.这类问题称为回归问题. 回归问题(Regression) 是给定多个自变量.一个因变 ...