您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關python中Todo清單的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
Todo清單
需要實現的功能有添加任務、刪除任務、編輯任務,操作要關聯數據庫。
任務需要綁定用戶,部門。用戶需要綁定部門。
{#自己編寫一個基類模板#} {% extends 'bootstrap/base.html' %} {% block styles %} {{ super() }} <link rel="stylesheet" href="../static/css/main.css" rel="external nofollow" > {% endblock %} {% block navbar %} <nav class="navbar navbar-default"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="index.html" rel="external nofollow" ></a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁<span class="sr-only">(current)</span></a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >新聞</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >國際</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >國內</a></li> <li><a href="/sysinfo/" rel="external nofollow" >系統信息</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >登陸用戶</a></li> </ul> <ul class="nav navbar-nav navbar-right"> {% if 'user' in session %} <li><a href="login.html" rel="external nofollow" ><span class="glyphicon glyphicon-user"></span> {{ session.user }}</a></li> <li><a href="/logout/" rel="external nofollow" ><span class="glyphicon glyphicon-log-in"></span> 注銷 </a></li> {% else %} <li><a href="/login/" rel="external nofollow" ><span class="glyphicon glyphicon-log-in"></span> 登陸</a></li> {% endif %} <li><a href="/register/" rel="external nofollow" ><span class="glyphicon glyphicon-log-out"></span> 注冊</a></li> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> {% endblock %} {% block content %} {#定義屬于自己的block#} {% block newcontent %} {% endblock %} {% block footer %} <div class="footer" > 宇宙大魔王 </div> {% endblock %} {% endblock %}
{#列表清單#} {% extends 'base.html' %} {% block newcontent %} <div class="row"> <div class="col-md-6 col-md-offset-3"> /*添加任務*/ <form class="form-horizontal" action="/todo/add/" method="post"> <div class="form-group"> {# 添加框 #} <div class="col-sm-9"> <input type="text" class="form-control" placeholder="請添加任務" required="required" name="todo_name"> </div> {# 選擇框 #} <div class="col-sm-2"> <select class="form-control" name="part"> {% for part in parts %} <option value="{{ part.id }}">{{ part.name }}</option> {% endfor %} </select> </div> {# 添加的按鈕 #} <div class="col-sm-1"> <input type="submit" class="btn btn-success" value="添加任務"> </div> </div> </form> /*任務顯示*/ <h2>添加任務</h2> <table class="table table-bordered"> <tr> <td>任務內容</td> <td>創建時間</td> <td>狀態</td> <td>所屬部門</td> <td>操作</td> </tr> {% for todo in todos %} <tr> <td>{{ todo.name }}</td> <td>{{ todo.add_time }}</td> {# #} <td> {% if todo.status %} <a href="/todo/undo/{{ todo.id }}/" rel="external nofollow" class="btn btn-sm btn-success" role="button"><span class="glyphicon glyphicon-remove"></span> 已完成</a> {% else %} <a href="/todo/done/{{ todo.id }}/" rel="external nofollow" class="btn btn-sm btn-warning" role="button"><span class="glyphicon glyphicon-remove"></span> 未完成</a> {% endif %} </td> <td>{{ todo.depart.name }}</td> <td> {# <a href="/todo/delete/{{ todo.id }}/" rel="external nofollow" rel="external nofollow" role="button">刪除</a>#} {# <a href="/todo/delete/{{ todo.id }}/" rel="external nofollow" rel="external nofollow" class="btn btn-primary btn-lg active" role="button">刪除</a>#} <a href="/todo/delete/{{ todo.id }}/" rel="external nofollow" class="btn btn-danger" role="button"><span class="glyphicon glyphicon-remove"></span> 刪除</a> </td> </tr> {% endfor %} </table> </div> </div> {% 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 '<User:%s>' %(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 '<Depart:%s>' %(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 '<Todo:%s>' % (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/<int:id>/') 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/<int:id>/') 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/<int:id>/') 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 頁面最初顯示圖
當添加任務后,頁面顯示如下
當鼠標點擊任務狀態時,會發生改變
當點擊刪除按鈕時,任務從數據庫中刪除,也不在頁面中顯示
關于“python中Todo清單的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。