溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python中Todo清單的示例分析

發(fā)布時(shí)間:2021-08-12 13:44:50 來源:億速云 閱讀:122 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)python中Todo清單的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

Todo清單

需要實(shí)現(xiàn)的功能有添加任務(wù)、刪除任務(wù)、編輯任務(wù),操作要關(guān)聯(lián)數(shù)據(jù)庫。

任務(wù)需要綁定用戶,部門。用戶需要綁定部門。

{#自己編寫一個(gè)基類模板#}
{% 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" >國內(nèi)</a></li>
        <li><a href="/sysinfo/" rel="external nofollow" >系統(tǒng)信息</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>
          &nbsp;&nbsp; {{ session.user }}</a></li>
        <li><a href="/logout/" rel="external nofollow" ><span class="glyphicon glyphicon-log-in"></span>
          &nbsp;&nbsp; 注銷 </a></li>


        {% else %}

        <li><a href="/login/" rel="external nofollow" ><span class="glyphicon glyphicon-log-in"></span>
          &nbsp;&nbsp;登陸</a></li>
        {% endif %}


        <li><a href="/register/" rel="external nofollow" ><span class="glyphicon glyphicon-log-out"></span>
          &nbsp;&nbsp;注冊(cè)</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">
      /*添加任務(wù)*/
      <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="請(qǐng)?zhí)砑尤蝿?wù)" 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="添加任務(wù)">
          </div>
        </div>

      </form>


      /*任務(wù)顯示*/
      <h2>添加任務(wù)</h2>
      <table class="table table-bordered">
        <tr>
          <td>任務(wù)內(nèi)容</td>
          <td>創(chuàng)建時(shí)間</td>
          <td>狀態(tài)</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 %}
# 數(shù)據(jù)庫操作文件 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與其他表關(guān)聯(lián)
  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與其他表關(guān)聯(lián)
  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開發(fā)部','Java開發(fā)部']
  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()
# 試圖函數(shù)程序 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文件中表單定義的添加任務(wù)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清單的示例分析

當(dāng)添加任務(wù)后,頁面顯示如下

python中Todo清單的示例分析

當(dāng)鼠標(biāo)點(diǎn)擊任務(wù)狀態(tài)時(shí),會(huì)發(fā)生改變

python中Todo清單的示例分析

當(dāng)點(diǎn)擊刪除按鈕時(shí),任務(wù)從數(shù)據(jù)庫中刪除,也不在頁面中顯示

python中Todo清單的示例分析

關(guān)于“python中Todo清單的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI