Python的第一个web项目demo

copy了一个网上简单的python工程。
参照网址:https://jishuin.proginn.com/p/763bfbd2432b
工具PyCharm.
安装Python3.
安装sqlite3,创建数据库todo.db,创建todo表:

CREATE TABLE todo(
   id integer PRIMARY KEY autoincrement,
   task CHAR(50) NOT NULL,
   status CHAR(50) NOT NULL
);


Python引入bottle包--web框架。

重新敬上代码

todolist.py

import sqlite3
from bottle import route, run, template, request, error


@route('/todo')
def todo_list():
    conn = sqlite3.connect('todo.db')
    c = conn.cursor()
    c.execute("SELECT id, task FROM todo WHERE status LIKE '1'")
    result = c.fetchall()
    c.close()
    output = template('make_table', rows=result)
    return output


@route('/new', method='GET')
def new_task():
    if request.GET.save:

        new = request.GET.task.strip()
        conn = sqlite3.connect('todo.db')
        c = conn.cursor()

        c.execute("INSERT INTO todo (task,status) VALUES (?,?)", (new, 1))
        new_id = c.lastrowid

        conn.commit()
        c.close()

        return '<p>成功添加数据, ID 为: %s</p>' % new_id

    else:
        return template('new_task.tpl')

@route('/edit/<no:int>', method='GET')
def edit_item(no):

    if request.GET.save:
        edit = request.GET.task.strip()
        status = request.GET.status.strip()

        if status == 'open':
            status = 1
        else:
            status = 0

        conn = sqlite3.connect('todo.db')
        c = conn.cursor()
        c.execute("UPDATE todo SET task = ?, status = ? WHERE id LIKE ?", (edit, status, no))
        conn.commit()

        return '<p>成功提交计划 id:%s </p>' % no
    else:
        conn = sqlite3.connect('todo.db')
        c = conn.cursor()
        c.execute("SELECT task FROM todo WHERE id LIKE ?", (str(no),))
        cur_data = c.fetchone()

        return template('edit_task', old=cur_data, no=no)

@route('/json<json:re:[0-9]+>')
def show_json(json):
    conn = sqlite3.connect('todo.db')
    c = conn.cursor()
    c.execute("SELECT task FROM todo WHERE id LIKE ?", (json,))
    result = c.fetchall()
    c.close()

    if not result:
        return {'task': 'This item number does not exist!'}
    else:
        return {'task': result[0]} # 中文乱码可以使用 json 库解决:json.dumps(result,ensure_ascii=False)


@error(404)
def mistake404(code):
    return 'Sorry, 你要找的网页被狗子吃了哦!!'

run(reload=True, host='localhost', port=8888)

new_task.tpl

<p>添加一个你接下来要做的计划:</p>
<form action="/new" method="GET">
  <input type="text" size="100" maxlength="100" name="task">
  <input type="submit" name="save" value="提交">
</form>

make_table.tpl

<p>这是你接下来要做的事情:</p>
<table border="1">
%for row in rows:
  <tr>
  %for col in row:
    <td>{{col}}</td>
  %end
  </tr>
%end
</table>

edit_task.tpl

<p>编辑计划  ID = {{no}}</p>
<form action="/edit/{{no}}" method="get">
  <input type="text" name="task" value="{{old[0]}}" size="100" maxlength="100">
  <select name="status">
    <option>未完成</option>
    <option>已完成</option>
  </select>
  <br>
  <input type="submit" name="save" value="提交">
</form>



posted @ 2021-01-22 18:03  倔强的老铁  阅读(756)  评论(0编辑  收藏  举报