I
I
IceTony2016-11-29 22:19:14
Python
IceTony, 2016-11-29 22:19:14

How to edit database from web form?

Good day, help me understand this task:
There is a database, the data is displayed in the action calendar (html table), when you click on the action, you need to open the form for editing (so that you can change the status and performer of this action)
I.e. clicked on the action, changed the artist, status through the drop-down list and clicked “Save”
As I understand it, it is necessary to somehow transfer data to sql through the POST method?

from http.server import BaseHTTPRequestHandler, HTTPServer
import sqlite3
 
 
html_text = '''<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>Календарь событий</title>
  <style>
   #time {{
    width: 80px;
    }}
   #task {{
   width: 160px;
   }}
   #status {{
    width: 100px;
   }}
   #executor {{
    width: 50px;
   }}
   table {{
    background: white;
    color: black;
    border-spacing: 0px;
   }}
   td, th {{
    padding: 5px;
   }}
   th {{
       background: #00BFFF;
   }}
  </style>
 </head>
 <body>
  <table border="1">
   <tr>
    <th id="time">Время</th>
    <th id="task">Задача</th>
    <th id="status" >Статус</th>
    <th id="executor">Исполнитель</th>
   </tr>
   {html_rows}
 </table>
 </body>
</html>'''
 
row = '''
<tr>
    <td>{task_time}</td>
    <td>{task}</td>
    <td>{status}</td>
    <td>{executor}</td>
</tr>
'''
 
 
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
 
        conn = sqlite3.connect('db.db')
        cur = conn.cursor()
 
        cur.execute('SELECT * FROM tasks;')
        rows = cur.fetchall()
 
        html_rows = str()
 
        for task_time, task, status,executor in rows:
            html_rows += row.format(task_time=task_time, task=task, status=status, executor=executor)
 
        self.wfile.write(bytes(html_text.format(html_rows=html_rows), "utf8"))
        return
 
 
def run():
    print('starting server...')
    server_address = ('127.0.0.1', 8081)
    httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
    print('running server...')
    httpd.serve_forever()
 
run()

--
-- Файл сгенерирован с помощью SQLiteStudio v3.1.1 в Вт ноя 29 15:43:33 2016
--
-- Использованная кодировка текста: System
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;
-- Таблица: executors
CREATE TABLE executors (executor STRING PRIMARY KEY);
INSERT INTO executors (executor) VALUES ('Папа');
INSERT INTO executors (executor) VALUES ('Мама');
INSERT INTO executors (executor) VALUES ('Сын');
INSERT INTO executors (executor) VALUES ('Дочь');
-- Таблица: statuss
CREATE TABLE statuss (status STRING PRIMARY KEY);
INSERT INTO statuss (status) VALUES ('Не выполнено');
INSERT INTO statuss (status) VALUES ('Выполнено');
-- Таблица: tasks
CREATE TABLE tasks (task_time DATETIME PRIMARY KEY, task STRING, status STRING REFERENCES statuss (status), executor STRING REFERENCES executors (executor));
INSERT INTO tasks (task_time, task, status, executor) VALUES ('2016-11-28 14:00:00', 'Сходить в магазин', 'Выполнено', 'Мама');
INSERT INTO tasks (task_time, task, status, executor) VALUES ('2016-11-28 17:00:00', 'Помыть машину', 'Выполнено', 'Папа');
INSERT INTO tasks (task_time, task, status, executor) VALUES ('2016-11-29 08:30:00', 'Сделать зарядку', 'Выполнено', 'Папа');
INSERT INTO tasks (task_time, task, status, executor) VALUES ('2016-11-29 10:00:00', 'Отвести сына в школу', 'Выполнено', 'Мама');
INSERT INTO tasks (task_time, task, status, executor) VALUES ('2016-11-30 19:00:00', 'Поиграть в игру', 'Не выполнено', 'Сын');
INSERT INTO tasks (task_time, task, status, executor) VALUES ('2016-11-30 20:00:00', 'Почитать книгу', 'Не выполнено', 'Дочь');
INSERT INTO tasks (task_time, task, status, executor) VALUES ('2016-12-01 07:00:00', 'Поехать на рыбалку', 'Не выполнено', 'Папа');
INSERT INTO tasks (task_time, task, status, executor) VALUES ('2016-12-01 13:00:00', 'Сделать маникюр', 'Не выполнено', 'Мама');
INSERT INTO tasks (task_time, task, status, executor) VALUES ('2016-12-02 17:00:00', 'Сделать уроки', 'Не выполнено', 'Сын');
INSERT INTO tasks (task_time, task, status, executor) VALUES ('2016-12-02 18:00:00', 'Посетить кружок танцев', 'Не выполнено', 'Дочь');
COMMIT TRANSACTION;
PRAGMA foreign_keys = on;

f9v67n6dJY.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Konovalov, 2016-11-30
@akonovalov

I suggest not to suffer and not "bike", but switch to Django - among other things, there is a relatively normal admin panel, you can edit the database in it.

M
Max, 2016-11-29
@MaxDukov

via POST / GET you will transfer the data to the script, which will parse them and do what you program in it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question