I am a beginner in this field and in the process of preparing a final project for a CS50 course using Flask, Python and SQLite 3 The error appears when I try to delete the question whose position is first in the list, but the code works for the rest of the questions that are in different indexes I don't know how to solve this bug please help!! Thank you in advance
I have tried to print the id for each question to be deleted. Everyone is printed except for the first one print None, which gives me the error, even though it appears in the application interface.
this is my app.py code
@app.route("/list", methods=["GET", "POST"])
def list():
if request.method == "POST":
category = request.form.get("language")
if category == "All":
data = db.execute("SELECT * FROM questions")
question = []
answer = []
index = []
for item in data:
question.append(item["question"])
answer.append(item["answer"])
index.append(item["id"])
return render_template("list.html", data=zip(question,answer,index))
if request.form.get("language") == category:
data = db.execute("SELECT * FROM questions WHERE language=?", category)
question = []
answer = []
index = []
for item in data:
question.append(item["question"])
answer.append(item["answer"])
index.append(item["id"])
return render_template("list.html", data=zip(question,answer,index))
else:
data = db.execute("SELECT * FROM questions")
question = []
answer = []
index = []
for item in data:
question.append(item["question"])
answer.append(item["answer"])
index.append(item["id"])
return render_template("list.html", data=zip(question,answer,index))
@app.route("/delete", methods=["GET", "POST"])
def delete():
if request.method == "POST":
index = request.form["delete"]
print(index)
db.execute("DELETE FROM questions WHERE id=?", index)
flash("Questio deleted")
db.execute("UPDATE questions SET id = id - 1 WHERE id >?", index)
return redirect("/")
else:
data = db.execute("SELECT * FROM questions")
question = []
answer = []
index = []
for item in data:
question.append(item["question"])
answer.append(item["answer"])
index.append(item["id"])
return render_template("list.html", data=zip(question,answer,index))
and this is my list.html
{% extends "layout.html" %}
{% block title %}
list
{% endblock %}
{% block main %}
<form action="{{ url_for('list')" method="POST">
<div class="nestedGrid">
<div class="language">
<h2>Languages</h2>
<button class="list-item" name="language" value="All">ALL</button> <br>
<button class="list-item" name="language" value="python">PYTHON</button> <br>
<button class="list-item" name="language" value="c">C</button> <br>
<button class="list-item" name="language" value="javaScript">JS</button> <br>
<button class="list-item" name="language" value="java">JAVA</button> <br>
</div>
<div class="question">
<h2>Questions</h2>
<ul class="list">
{% for i, j, z in data %}
<li class="list-item panel" name=question_deleted>{{ i }}
<span class="tooltiptext">Show answer</span>
</li>
<li class="list-item panel">{{ j }}</li>
<form action="{{ url_for('delete')" method="post">
<button class="list-item" name="delete" value="{{ z }}">{{ z }} <i class="fa fa-trash"></i> </button>
</form>
{% endfor %}
</ul>
</div>
</div>
</form>
{% endblock %}
i attached screen shots of my code and my frontend app is in this repo: screen shots