I am building a partial website with others but when I want to delete a checked item, it gives an error "Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."
the url is http://127.0.0.1:8080/delete
here are my scripts:
init.py
from flask import Flask, render_template,request, flash, redirect, url_for
from flask_mysqldb import MySQL, MySQLdb
app = Flask(__name__)
app.config['SECRET_KEY'] = 'IFT402'
app.config['MYSQL_HOST'] = "localhost"
app.config['MYSQL_USER'] = "root"
app.config['MYSQL_PASSWORD'] = ""
app.config['MYSQL_DB'] = "webapp"
mysql = MySQL(app)
@app.route('/', methods=['GET','POST'])
def home():
if request.method == 'POST':
pass
return render_template ('home.html')
@app.route('/newMovement', methods=['GET', 'POST'])
def newMovement():
if request.method == 'POST':
assetID = request.form['assetID']
issueDate = request.form['issueDate']
Location = request.form['Location']
EmployeeName = request.form['EmployeeName']
returnDate = request.form['returnDate']
details = request.form['details']
returnStatus = request.form['returnStatus']
cur = mysql.connection.cursor()
try:
cur.execute("INSERT INTO items (assetID, issueDate, Location, EmployeeName, returnDate, details, returnStatus) VALUES (%s,%s,%s,%s,%s,%s,%s)", (assetID, issueDate, Location, EmployeeName, returnDate, details, returnStatus))
mysql.connection.commit()
flash('Item added successfully', 'success')
except Exception as e:
flash(f'Error: {str(e)}', 'error')
finally:
cur.close()
return render_template('newMovement.html')
@app.route('/transfer')
def items():
cur = mysql.connection.cursor()
#cur.execute("SELECT * FROM items")
#itemDetails = cur.fetchall()
#cur.close()
#return render_template('transfer.html', itemDetails=itemDetails)
items = cur.execute("SELECT * FROM items")
if items > 0:
itemDetails = cur.fetchall()
return render_template('transfer.html', itemDetails=itemDetails)
@app.route('/newrestock', methods=['GET', 'POST'])
def newRestock():
if request.method == 'POST':
assetID = request.form['assetID']
restockDate = request.form['restockDate']
Quantity = request.form['Quantity']
detailsRestock = request.form['detailsRestock']
cur = mysql.connection.cursor()
try:
cur.execute("INSERT INTO restock (assetID, restockDate, Quantity, detailsRestock) VALUES (%s,%s,%s,%s)", (assetID, restockDate, Quantity, detailsRestock))
mysql.connection.commit()
flash('Restock added successfully', 'success')
except Exception as e:
flash(f'Error: {str(e)}', 'error')
finally:
cur.close()
return render_template('newRestock.html')
@app.route('/restock')
def restock():
cur = mysql.connection.cursor()
restock = cur.execute("SELECT * FROM items")
if restock > 0:
restockDetails = cur.fetchall()
return render_template('restock.html', restockDetails=restockDetails)
@app.route('/delete/<int:id>', methods=['GET','POST'])
def deleteMovement(id):
cur = mysql.connection.cursor()
cur.execute("DELETE FROM items WHERE id = %s", (id,))
mysql.connection.commit()
cur.close()
flash('Item sucessfully deleted', 'success')
return redirect('/transfer')
if __name__ == "__main__":
app.run(debug=True, port=8080, use_reloader=False)
transfer.html
{% extends "base.html" %}
{% block title %} Transfer {% endblock %}
{% block content %}
<style>
table {
width: 100%;
border-collapse: collapse;
}
th,td {
text-align: left;
padding: 8px;
border: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
.button-container {
display: flex;
}
.button {
margin: 5px;
}
</style>
<h1>Digitalize Asset Tracker for P.T. Invosa System</h1>
<form action="/search" method="GET">
<input type="text" name="query" placeholder="INFORMATION>>>>>>">
<div class="button-container">
<a href="{{ url_for('newMovement') }}" class="btn btn-primary">Add Movement</a>
<form action="/delete/{{ assetID }}" method="post">
<input type = "submit" value="Delete">
</form>
<button type="submit">Activity Report</button>
</div>
</form>
<h2 align="center">Asset List</h2>
<!--<form method = "POST" action="/delete"> -->
<table border="1">
<thead>
<tr>
<th>Asset ID</th>
<th>Issue Date</th>
<th>Location</th>
<th>Employee Name</th>
<th>Return Date</th>
<th>Details</th>
<th>Return Status</th>
<th>Select</th>
</tr>
</thead>
<tbody>
{% for items in itemDetails %}
<tr id="{{ items.id }}">
<td>{{ items[0] }}</td>
<td>{{ items[1] }}</td>
<td>{{ items[2] }}</td>
<td>{{ items[3] }}</td>
<td>{{ items[4] }}</td>
<td>{{ items[5] }}</td>
<td>{{ items[6] }}</td>
<td><input type="checkbox" name="mycheckbox" value="{{ items.id }}"></td>
</tr>
{% endfor %}
</tbody>
</table>
<form method ="POST" action="/delete">
<button type = submit class="btn btn-danger">Delete</button>
</form>
{% endblock %}
I tried changing the delete route but was not successful in troubleshooting.
I don't think that you are passing in the id for your route. If you use url_for() you can pass in the id field to your python deleteMovement function using jinja.
<form method ="POST" action="{{ url_for('deleteMovement', id=items.id) }}">
<button type = submit class="btn btn-danger">Delete</button>
</form>
It would create a url like http://127.0.0.1:8080/delete/1 if the items.id was 1.
Put this form inside of your for loop however so that you have a separate delete button for each item that you are displaying in your table.