Python基于Flask,ThinkPHP开发简易个人博客
2019-06-14 17:34
651 查看
Flask是一款基于Python的轻量级后端框架
下载链接:
Flask官网:http://flask.pocoo.org/docs/0.10/
其他流行的Python框架:Django:https://www.djangoproject.com/
基于ThinkPHP和基于Flask的简易博客下载地址
https://download.csdn.net/download/weixin_43746433/11241631
1.项目准备
准备项目:templates/,config.py、run.py,数据库
表单实现
处理表单并跳转
文件目录
2.数据库配置:
虚拟环境我使用的是以前PHP开发使用的wampserver: http://www.wampserver.com/
config.py文件
HOST = 'localhost' PORT = 8889 USER = 'root' PASSWORD = 'root' DATABASE = 'easy_blog' CHARSET = 'utf8'
from flask import * import warnings warnings.filterwarnings("ignore") import pymysql # 提供了许多函数和变量来处理 Python 运行时环境的不同部分. import importlib,sys importlib.reload(sys) #import MySQLdb.cursors import datetime import os from datetime import timedelta app = Flask(__name__) #静态文件缓存时间设置 app.config['SEND_FILE_MAX_AGE_DEFAULT']=timedelta(seconds=1) app.config.from_object(config) app.config['SECRET_KEY']=os.urandom(24) #设置为24位的字符,每次运行服务器都是不同的,所以服务器启动一次上次的session就清除。 app.config['PERMANENT_SESSION_LIFETIME']=timedelta(days=7) #设置session的保存时间。 app.config.from_object(__name__) #coding=utf8 # 连接数据库 def connectdb(): db = pymysql.connect(host='localhost', user='root', passwd='root', db='easy_blog',charset='utf8') db.autocommit(True) cursor = db.cursor() return (db,cursor) # 关闭数据库 def closedb(db,cursor): db.close() cursor.close() # 首页 @app.route('/') def index(): return render_template('index.html') # 处理表单提交 @app.route('/handle', methods=['POST']) def handle(): dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print(dt) # 获取post数据 data = request.form print(data['title']) print(data['content']) # 连接数据库 (db,cursor) = connectdb() a=1 # 添加数据 ##, str(int(time.time()))]) sql="insert into post(title, content, timestamp) values('%s', '%s', '%s')" %(data['title'], data['content'],dt) cursor.execute(sql) result=cursor.fetchone() if (result is None): print('flase') else: print('插入') # 最后添加行的id db.commit() post_id = cursor.lastrowid print(post_id) cursor.execute("update post set id='%s' where title='%s'" %(post_id,data['title'])) db.commit() # 关闭数据库 closedb(db,cursor) return redirect(url_for('post', post_id=post_id)) # 文章列表页 @app.route('/list') def list(): # 连接数据库 (db,cursor) = connectdb() # 获取数据 cursor.execute("select * from post order by id desc") posts = cursor.fetchall() ## 格式化时间戳 #for x in range(0, len(posts)): # posts[x][2] = time.strftime('%Y-%m-%d %H:%M:%S',posts[x][2]) # 关闭数据库 closedb(db,cursor) # 后端向前端传递数据 return render_template('list.html', posts=posts) # 文章详情页 @app.route('/post/<post_id>') def post(post_id): # 连接数据库 (db,cursor) = connectdb() # 查询数据 cursor.execute("select * from post where id=%s" %(post_id)) post = cursor.fetchone() # 格式化时间戳 #post['timestamp'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(post['timestamp']))) # 关闭数据库 closedb(db,cursor) # 后端向前端传递数据 return render_template('post.html', post=post) @app.route('/delete/<post_id>') def delete(post_id): print('post_id',post_id) print('666') (db, cursor) = connectdb() cursor.execute("delete from post where id=%s" % (post_id)) db.commit() result=cursor.fetchone() if (result is None): print('flase') else: print('插入') return redirect(url_for('list')) #return render_template('list.html') #<h4><a href="{{url_for('post', post_id=item[3])}}">{{item[0]}}</a></h4> if __name__ == '__main__': app.run(debug=True)
运行结果:
界面:
博客内容:
查看列表:
删除博客:
相关文章推荐
- Python 语言 Oracle 数据库基于 SQLAlchemy + Flask + Ajax 的 Json 前后端分离开发博客系统遇到的坑汇总
- 用flask开发个人博客(20)—— 利用模型创建数据库
- 基于Flask框架的Python web程序的开发实战 <二> 项目组织结构
- 用flask开发个人博客(22)—— 使用Flask-Migrate实现数据库的更新
- 用flask开发个人博客(31)——用flask_login管理用户的登录
- 用flask开发个人博客(25)—— 博客程序结构介绍
- 用flask开发个人博客(6)—— 为flask程序添加命令行解释器
- 基于ThinkPHP3.1.2框架开发的WBlog博客程序
- 用flask开发个人博客(36)—— 使用SQLAlchemy对博客文章进行分页
- 用flask开发个人博客(12)—— 自定义错误界面
- 用flask开发个人博客(28)—— 利用unittest进行单元测试
- Django开发个人博客网站——27、服务器centos7安装Python3.6 与 pip3
- 用flask开发个人博客(8)—— flask中的模板
- 用flask开发个人博客(10)—— Jinja2模板中的控制结构
- 简易博客开发(8)----django1.9 博客部署到pythonanywhere上
- 基于ssm框架的个人博客系统(10)--编写博客页面开发
- 基于AspNet Core2.0 开发框架,包含简单的个人博客Demo
- 用flask开发个人博客(39)—— 在flask中定义Rest API
- 用flask开发个人博客(11)—— 模板中代码的重复使用
- java毕设--基于ssh框架开发的个人博客系统