目录
前言
一、结果图
1.首页和目录
2.增
3.删
4.改
5.查看
6.关键字查询
二、使用步骤
1.首页和目录
2.增
3.删
4.改
5.查
6.搜索
三、全部代码
整个项目
1.app.py
2.config.py
3.add.html
4.afteradd.html
5.afterdelete.html
6.aftermodify.html
7.aftersearch.html
8.base.html
9.delete.html
10.display.html
12.error.html
13.index.html
15.modify.html
16.search.html
总结:
前言
我是一个大一刚开始敲代码没多久的新生,还是学校工作室里的考核,以此来记录一下自己的学习历程。我对于很多东西理解浅薄,所以代码实现的是挺复杂的,因为掌握的实在不咋地嘛,要是有错误还请多多指教。
我这里运用到的知识的话,数据库基本的增删改查操作,render_template()的使用,一点HTML的知识
一、结果图
1.首页和目录
2.增
3.删
4.改
5.查看
3.删
4.改
5.查看
5.查看
6.关键字查询
二、使用步骤
1.首页和目录
1.首页和目录
2.增
3.删
4.改
5.查
6.搜索
三、全部代码
整个项目
5.查
6.搜索
三、全部代码
整个项目
三、全部代码
整个项目
1.app.py
from flask import Flask,redirect,request,jsonify,abort,render_template,url_for
from flask_sqlalchemy import SQLAlchemy
import datetime
import config
import pprint
app = Flask(__name__)
app.config.from_object(config)
db=SQLAlchemy(app)
class Database(db.Model):
__tablename__='todolist'
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
title = db.Column(db.String(200))
content=db.Column(db.Text)
status=db.Column(db.String(5))
addtime=db.Column(db.String(20),default=datetime.datetime.now)
deadline=db.Column(db.String(20))
db.create_all()
@app.errorhandler(404)
def error(e):
return render_template('error.html')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/menu/')
def menu():
return render_template('menu.html')
@app.route('/add',methods=['GET','POST'])
def add():
if request.method=='GET':
return render_template('add.html')
if request.method=='POST':
c={}
item=request.form.to_dict()
for i,j in item.items():
c[i]=j
title=c['title']
content=c['content']
status=c['status']
deadline=c['deadline']
addtime=str(datetime.datetime.now())[:16]
print(title,content,status,addtime,deadline)
engine = db.get_engine()
with engine.connect() as conn:
conn.execute(f"insert into todolist(title,content,status,addtime,deadline) values('{title}','{content}','{status}','{addtime}','{deadline}')")
return render_template('afteradd.html')
@app.route('/delete',methods=['GET','POST'])
def delete():
#1.显示所有的代办,输入title删除
# 获取数据库中信息,放到这个字典里面(放到todo里面!!!!!!)
content = {
'todo': {
}
}
engine = db.get_engine()
with engine.connect() as conn:
result = conn.execute('select * from todolist')
all = result.fetchall()
for i in all:
content['todo'][i[0]]={'title':i[1],'content':i[2],'status':i[3],'addtime':i[4],'deadline':i[5]}
if request.method=='GET':
return render_template('delete.html',**content)
if request.method=='POST':
item = request.form.to_dict()
flag = False
if item['choice']=='search':
title = item['title']
for i in content['todo']:
if content['todo'][i]['title'] == title:
with engine.connect() as conn:
conn.execute(f"delete from todolist where title='{title}'")
flag = True
else:
choice = item['choice']
if choice == 'all':
with engine.connect() as conn:
conn.execute(f"delete from todolist ")
flag = True
elif choice == 'alltodo':
with engine.connect() as conn:
conn.execute(f"delete from todolist where status='to do'")
flag = True
elif choice == 'alldone':
with engine.connect() as conn:
conn.execute(f"delete from todolist where status='done'")
flag = True
if flag==False:
abort(404)
return render_template('afterdelete.html')
@app.route('/modify',methods=['GET','POST'])
def modify():
content = {
'todo': {
}
}
engine = db.get_engine()
with engine.connect() as conn:
result = conn.execute('select * from todolist')
all = result.fetchall()
for i in all:
content['todo'][i[0]] = {'title': i[1], 'content': i[2], 'status': i[3], 'addtime': i[4], 'deadline': i[5]}
if request.method=='GET':
return render_template('modify.html',**content)
elif request.method=='POST':
ret=request.form.to_dict()
#得到修改的指令
if ret['status'] == 'done':
commend = 'done'
condition = 'to do'
elif ret['status'] == 'to do':
commend = 'to do'
condition = 'done'
#对数据库进行操作
title=ret['title']
engine = db.get_engine()
with engine.connect() as conn:
if ret['num']=='one':
conn.execute(f"update todolist set status='{commend}' where status='{condition}' and title='{title}';")
elif ret['num']=='all':
conn.execute(f"update todolist set status='{commend}' where status='{condition}';")
return render_template('aftermodify.html')
@app.route('/search',methods=['GET','POST'])
def search():
if request.method=='GET':
return render_template('search.html')
if request.method == 'POST':
content = {
'todo': {
}
}
engine = db.get_engine()
with engine.connect() as conn:
result = conn.execute('select * from todolist')
all = result.fetchall()
for i in all:
content['todo'][i[0]] = {'title': i[1], 'content': i[2], 'status': i[3], 'addtime': i[4],
'deadline': i[5]}
keyword = request.form.to_dict()
flag = False
for i in list(content['todo']):
if keyword['keyword'] in content['todo'][i]['content']:
flag = True
elif keyword['keyword'] in content['todo'][i]['title']:
flag=True
else:
del content['todo'][i]
if flag==False:
abort(404)
return render_template('aftersearch.html')
@app.route('/display/')
def display():
return render_template('display_menu.html')
@app.route('/display/all')
def display_all():
content={
'todo': {
}
}
#获取数据库中信息,放到这个字典里面(放到todo里面!!!!!!)
engine = db.get_engine()
with engine.connect() as conn:
result = conn.execute('select * from todolist')
all = result.fetchall()
for i in all:
content['todo'][i[0]]={'title':i[1],'content':i[2],'status':i[3],'addtime':i[4],'deadline':i[5]}
return render_template('display_all.html',**content)
@app.route('/display/todo')
def display_todo():
content = {
'todo': {
}
}
# 获取数据库中信息,放到这个字典里面(放到todo里面!!!!!!)
engine = db.get_engine()
with engine.connect() as conn:
result = conn.execute('select * from todolist')
all = result.fetchall()
for i in all:
if i[3]=='to do':
content['todo'][i[0]] = {'title': i[1], 'content': i[2], 'status': i[3], 'addtime': i[4], 'deadline': i[5]}
return render_template('display_all.html', **content)
@app.route('/display/done')
def display_done():
content = {
'todo': {
}
}
# 获取数据库中信息,放到这个字典里面(放到todo里面!!!!!!)
engine = db.get_engine()
with engine.connect() as conn:
result = conn.execute('select * from todolist')
all = result.fetchall()
for i in all:
if i[3] == 'done':
content['todo'][i[0]] = {'title': i[1], 'content': i[2], 'status': i[3], 'addtime': i[4], 'deadline': i[5]}
return render_template('display_all.html', **content)
if __name__ == '__main__':
app.run()
2.config.py
JSON_AS_ASCII=False
#数据库
USERNAME = 'root'
PASSWORD = '123456'
HOST = '127.0.0.1'
PORT = '3306'
DATAbase = 'todo'
SQLALCHEMY_DATAbase_URI = "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format( USERNAME, PASSWORD, HOST, PORT,DATAbase)
SQLALCHEMY_TRACK_MODIFICATIONS=False
3.add.html
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
添加代办
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
添加代办
{% endblock %}
4.afteradd.html
TODO
TODOLIST
-
回到首页
-
继续添加
5.afterdelete.html
TODO
TODOLIST
删除成功
没有该代办
{% endif %}
-
回到首页
6.aftermodify.html
TODO
TODOLIST
修改成功
-
回到首页
7.aftersearch.html
TODO
TODOLIST
{% if flag==True %}
找到了
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
{% else %}
没有找到
{% endif %}
-
回到首页
-
继续查询
8.base.html
TODO
TODOLIST
{% if flag==True %}
找到了
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
{% else %}
没有找到
{% endif %}
-
回到首页
-
继续查询
9.delete.html
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
删除代办
{% endblock %}
TODO
TODOLIST
删除成功
没有该代办
{% endif %}
- 回到首页
6.aftermodify.html
TODO
TODOLIST
修改成功
-
回到首页
7.aftersearch.html
TODO
TODOLIST
{% if flag==True %}
找到了
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
{% else %}
没有找到
{% endif %}
-
回到首页
-
继续查询
8.base.html
TODO
TODOLIST
{% if flag==True %}
找到了
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
{% else %}
没有找到
{% endif %}
-
回到首页
-
继续查询
9.delete.html
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
删除代办
TODO
TODOLIST
{% if flag==True %}
找到了
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
{% else %}
没有找到
{% endif %}
- 回到首页
- 继续查询
8.base.html
TODO
TODOLIST
{% if flag==True %}
找到了
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
{% else %}
没有找到
{% endif %}
-
回到首页
-
继续查询
9.delete.html
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
删除代办
找到了
-
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
没有找到
{% endif %}{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
删除代办
10.display.html
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
查看
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
{% endblock %}
-
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
11.display_menu.html
TODO
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
ul.pagination li {display: inline;}
ul.pagination li a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
}
TODOLIST
- 查看所有事项
- 查看未完成事项
- 查看已完成事项
- 查看所有事项
- 查看未完成事项
- 查看已完成事项
12.error.html
{% extends 'base.html' %}
{% block body %}
错误啦,没有这个事项哦
{% endblock %}
13.index.html
TODO
TODO LIST
这里是首页
目录
错误啦,没有这个事项哦
{% endblock %}
TODO
TODO LIST
这里是首页
目录
14.menu.html
{% extends 'base.html' %}
{% block title %}
menu
{% endblock %}
15.modify.html
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
修改代办
-
{% for hh in todo.values() %}
-
{% for j,k in hh.items() %}
{{ j }} : {{ k }}
{% endfor %}
{% endfor %}
{% endblock %}
16.search.html
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
查询代办:
{% endblock %}
总结:
哎,总之差不多就是这样啦,我知道没人能看完,因为我写的实在是太繁琐了,不过我才大一嘛,之后继续努力!!



