当前位置:   article > 正文

python清单全套教程_详解python Todo清单实战

python #todo

Todo清单

需要实现的功能有添加任务、删除任务、编辑任务,操作要关联数据库。

任务需要绑定用户,部门。用户需要绑定部门。

{#自己编写一个基类模板#}

{% extends 'bootstrap/base.html' %}

{% block styles %}

{{ super() }}

{% endblock %}

{% block navbar %}

{% endblock %}

{% block content %}

{#定义属于自己的block#}

{% block newcontent %}

{% endblock %}

{% block footer %}

{% endblock %}

{% endblock %}

{#列表清单#}

{% extends 'base.html' %}

{% block newcontent %}

/*添加任务*/

{# 添加框 #}

name="todo_name">

{# 选择框 #}

{% for part in parts %}

{{ part.name }}

{% endfor %}

{# 添加的按钮 #}

/*任务显示*/

添加任务

任务内容创建时间状态所属部门操作

{% for todo in todos %}

{{ todo.name }}{{ todo.add_time }}

{# #}

{% if todo.status %}

class="glyphicon glyphicon-remove">

已完成

{% else %}

class="glyphicon glyphicon-remove">

未完成

{% endif %}

{{ todo.depart.name }}

{# 删除#}

{# 删除#}

class="glyphicon glyphicon-remove">

删除

{% endfor %}

{% endblock %}

# 数据库操作文件 todo_models.py

from datetime import datetime

import pymysql

from flask import Flask

from flask_sqlalchemy import SQLAlchemy

from flask_bootstrap import Bootstrap

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:sheen@localhost/todo'

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

db = SQLAlchemy(app)

bootstrap = Bootstrap(app)

class User(db.Model):

id = db.Column(db.INTEGER,autoincrement=True,primary_key=True)

name = db.Column(db.String(30),unique=True)

pwd = db.Column(db.String(30))

add_time = db.Column(db.DateTime, default=datetime.now())

phone = db.Column(db.String(11))

email = db.Column(db.String(18),unique=True)

info = db.Column(db.TEXT)

department_id = db.Column(db.INTEGER,db.ForeignKey('department.id')) #部门id与其他表关联

todo_id = db.relationship('Todo',backref = 'user')

def __repr__(self):

return '' %(self.name)

class Department(db.Model):

id = db.Column(db.INTEGER,autoincrement=True,primary_key=True)

name = db.Column(db.String(30),unique=True)

users = db.relationship('User',backref = 'depart')

todos = db.relationship('Todo',backref = 'depart')

def __repr__(self):

return '' %(self.name)

class Todo(db.Model):

id = db.Column(db.INTEGER,autoincrement=True,primary_key=True)

name = db.Column(db.String(30))

add_time = db.Column(db.DateTime, default=datetime.now())

status = db.Column(db.Boolean, default=False)

department_id = db.Column(db.INTEGER,db.ForeignKey('department.id')) #部门id与其他表关联

user_id = db.Column(db.INTEGER,db.ForeignKey('user.id'))

def __repr__(self):

return '' % (self.name)

if __name__ == '__main__':

db.drop_all()

db.create_all()

parts = ['人事部','Python开发部','Java开发部']

partObj = [Department(name=part) for part in parts]

db.session.add_all(partObj)

db.session.commit()

user_1 = User(name='sheen',pwd='123',department_id=2)

db.session.add(user_1)

db.session.commit()

# 试图函数程序 todo_views.py

from flask import render_template, url_for, request, redirect

from todo_models import app,Todo,Department,db

@app.route('/')

def index():

return render_template('base.html')

@app.route('/login/')

def login():

return render_template('todo_login.html')

@app.route('/list/')

def todo_list():

todos = Todo.query.all()

parts = Department.query.all()

return render_template('todo_list.html',todos=todos,parts=parts)

@app.route('/todo/add/',methods=['POST'])

def add():

name = request.form['todo_name'] #在todo_list.html文件中表单定义的添加任务input属性name="todo_name"。

part = request.form['part']

todo = Todo(name=name,department_id=part,user_id=1)

db.session.add(todo)

db.session.commit()

print('ok')

return redirect(url_for('todo_list'))

@app.route('/todo/undo//')

def undo(id):

todo = Todo.query.filter_by(id=id).first()

todo.status = False

db.session.commit()

return redirect(url_for('todo_list'))

@app.route('/todo/done//')

def done(id):

todo = Todo.query.filter_by(id=id).first()

todo.status = True

db.session.commit()

return redirect(url_for('todo_list'))

@app.route('/todo/delete//')

def todo_del(id):

todo = Todo.query.filter_by(id=id).first()

db.session.delete(todo)

db.session.commit()

return redirect(url_for('todo_list'))

# 主程序 run.py

from flask import Flask

from todo_models import app

from todo_views import *

if __name__ == '__main__':

app.run()

list 页面最初显示图

64e81457ce372cc88e9d54fbc5aa595d.png

当添加任务后,页面显示如下

4de645a041e2965d766c888c3288709d.png

当鼠标点击任务状态时,会发生改变

4149e6aea21d9ddd7ee299dd81d8a946.png

当点击删除按钮时,任务从数据库中删除,也不在页面中显示

5409599fa113a7f4120e77136e64418d.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号