main.py
1from crypt import methods 2from turtle import title 3#db 追加 4from flaskr import app, db 5from flask import render_template, request, redirect, url_for 6 7from flaskr.db import DATABASE 8 9import sqlite3 10DATABASE = "database.db" 11 12 13@app.route('/') 14def index(): 15 con = sqlite3.connect(DATABASE) 16 db_books = con.execute("SELECT * FROM books").fetchall() 17 con.close 18 19 20 books = [] 21 for row in db_books: 22 books.append({"title": row[0], "price": row[1], "arrival_day": row[2]}) 23 24 return render_template( 25 'index.html',books=books 26 ) 27 28@app.route("/form") 29def form(): 30 return render_template("form.html") 31 32@app.route("/register", methods=["POST"]) 33def register(): 34 title = request.form["title"] 35 price = request.form["price"] 36 arrival_day =request.form["arrival_day"] 37 38 con = sqlite3.connect(DATABASE) 39 con.execute('INSERT INTO books VALUES(?,?,?)',[title,price,arrival_day]) 40 41 con.commit() 42 con.close() 43 return redirect(url_for("index")) 44 45
以上のコードに削除機能を追加したく質問させて頂きました。
main.py
1from crypt import methods 2from turtle import title 3#db 追加 4from flaskr import app, db 5from flask import render_template, request, redirect, url_for 6 7from flaskr.db import DATABASE 8 9import sqlite3 10DATABASE = "database.db" 11 12 13@app.route('/') 14def index(): 15 con = sqlite3.connect(DATABASE) 16 db_books = con.execute("SELECT * FROM books").fetchall() 17 con.close 18 19 20 books = [] 21 for row in db_books: 22 books.append({"title": row[0], "price": row[1], "arrival_day": row[2]}) 23 24 return render_template( 25 'index.html',books=books 26 ) 27 28@app.route("/form") 29def form(): 30 return render_template("form.html") 31 32@app.route("/register", methods=["POST"]) 33def register(): 34 title = request.form["title"] 35 price = request.form["price"] 36 arrival_day =request.form["arrival_day"] 37 38 con = sqlite3.connect(DATABASE) 39 con.execute('INSERT INTO books VALUES(?,?,?)',[title,price,arrival_day]) 40 41 con.commit() 42 con.close() 43 return redirect(url_for("index")) 44 45 46 47 48#以下追記した削除コードです 49@app.route('/delete/<int:id>', methods=['GET', 'POST']) 50def delete(id): 51 con = sqlite3.connect(DATABASE) 52 if request.method == 'POST': 53 con.execute('DELETE FROM books WHERE id = ?', [id]) 54 con.commit() 55 con.close() 56 return redirect(url_for('index')) 57 else: 58 book = con.execute('SELECT * FROM books WHERE id = ?', [id]).fetchone() 59 con.close() 60 if book: 61 return render_template('delete.html', book=book) 62 else: 63 return "Book not found", 404 64
db.py
1import sqlite3 2 3 4DATABASE = "database.db" 5 6def create_books_table(): 7 con = sqlite3.connect(DATABASE) 8 con.execute("CREATE TABLE IF NOT EXISTS books (title, price, arrival_day)") 9 con.close() 10 11 12
__init__.py
1from flask import Flask 2app = Flask(__name__) 3import flaskr.main 4 5from flaskr import db 6db.create_books_table()
index.html
1<!DOCTYPE html> 2<html lang="ja"> 3 4<head> 5 <meta charset="UTF-8"> 6 <title>sapu-app</title> 7 8 <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> 9</head> 10 11<body> 12 13 <h1>書店</h1> 14 <h2>今月の新刊一覧</h2> 15 {% if books == [] %} 16 <p>今月の新刊情報はまだありません</p> 17 {% else %} 18 <table border="1"> 19 <tr> 20 <th>入荷日</th> 21 <th>タイトル</th> 22 <th>金額</th> 23 </tr> 24 25 {% for book in books %} 26 <tr> 27 <td>{{ book.arrival_day }}</td> 28 <td>{{ book.title }}</td> 29 <td>{{ book.price }}円</td> 30 </tr> 31 {% endfor %} 32 </table> 33 {% endif %} 34 35 <a href= "{{ url_for('form') }}">編集</a> 36 37 38 39<!-- 削除用に追記したhtml --> 40 41 <table> 42 <thead> 43 <tr> 44 <th>Title</th> 45 <th>Price</th> 46 <th>Arrival Day</th> 47 <th>Actions</th> 48 </tr> 49 </thead> 50 <tbody> 51 {% for book in books %} 52 <tr> 53 <td>{{ book["title"] }}</td> 54 <td>{{ book["price"] }}</td> 55 <td>{{ book["arrival_day"] }}</td> 56 <td> 57 <a href="{{ url_for('delete', id=loop.index) }}">Delete</a> 58 </td> 59 </tr> 60 {% endfor %} 61 </tbody> 62 </table> 63 64 65 66 67 68</body> 69 70</html> 71 72
form.html
1<!DOCTYPE html> 2<html lang="ja"> 3 4<head> 5 <meta charset="UTF-8"> 6 <title>sapu-app</title> 7 8 <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> 9 10</head> 11 12<body> 13 <h1>書店</h1> 14 <form method="post" action="{{ url_for('register') }}"> 15 <table border="1"> 16 <tr> 17 <th>入荷日</th> 18 <th>タイトル</th> 19 <th>金額</th> 20 </tr> 21 <tr> 22 <td><input type="text" name="arrival_day"></td> 23 <td><input type="text" name="title"></td> 24 <td><input type="text" name="price"></td> 25 </tr> 26 </table> 27 <br> 28 <input type="submit" value="登録"> 29 </form> 30</body> 31 32</html> 33 34<!DOCTYPE html> 35<html lang="jp"> 36<head> 37 <meta charset="UTF-8"> 38 <title>Delete Book</title> 39</head> 40<body> 41 <h1>Delete Book</h1> 42 <p>Are you sure you want to delete the following book?</p> 43 <p>Title: {{ book[0] }}</p> 44 <p>Price: {{ book[1] }}</p> 45 <p>Arrival Day: {{ book[2] }}</p> 46 <form method="POST"> 47 <input type="submit" value="Delete"> 48 </form> 49</body> 50</html> 51 52
delete.html
1<!DOCTYPE html> 2<html lang="jp"> 3<head> 4 <meta charset="UTF-8"> 5 <title>Delete Book</title> 6</head> 7<body> 8 <h1>Delete Book</h1> 9 <p>Are you sure you want to delete the following book?</p> 10 <p>Title: {{ book[0] }}</p> 11 <p>Price: {{ book[1] }}</p> 12 <p>Arrival Day: {{ book[2] }}</p> 13 <form method="POST"> 14 <input type="submit" value="Delete"> 15 </form> 16</body> 17</html> 18 19
ローカルホストを立ち上げdeleteを押すと以下のエラーが出てきます。
OperationalError sqlite3.OperationalError: no such column: id Traceback (most recent call last) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 2213, in __call__ return self.wsgi_app(environ, start_response) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 2193, in wsgi_app response = self.handle_exception(e) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 2190, in wsgi_app response = self.full_dispatch_request() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 1486, in full_dispatch_request rv = self.handle_user_exception(e) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request rv = self.dispatch_request() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 1469, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) File "/Users/kojitakemura/Desktop/pythonProject1 2元本 2/flaskr/main.py", line 58, in delete book = con.execute('SELECT * FROM books WHERE id = ?', [id]).fetchone() sqlite3.OperationalError: no such column: id The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error. To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side. You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection: dump() shows all variables in the frame dump(obj) dumps all that's known about the object
vscodeで作業をしています。
Python 3.10.7 64bit です。
駆け出しの身でflaskのdeleteの書き方がわからずつまづいております。htmlもぐちゃぐちゃですがアドバイスいただきたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/10/02 16:55