My website is supposed to send data from a form through my app.py, which then forwards it to my database. In the terminal, I can see that it sends the POST to the app, but then the website itself 404's, saying "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again." in reference to app.py, and the database receives nothing.
HTML:
<!-- Voting Section HTML -->
<h1 style="text-align: center; margin-top: 30px;">VOTE NOW!!1!11!1!!!</h1>
<!-- Form for submitting data to table "voters" -->
<form style="text-align: center;" method="POST" action="/YR12--Assessments-Repository/Website/app.py">
<label>Name: <input type="text" name="name" required></label><br>
<label>Age: <input type="number" name="age" min="0" max="100" required></label><br>
<label for="candidate">Choose your candidate:</label>
<select name="candidate" id="candidate" required>
<option value="" disabled selected>Select a candidate</option>
<option value="id">John Smith</option>
</select>
<button style="margin-top: 10px;" id="log" type="submit">Submit</button>
</form>
</body>
</html>
app.py:
from flask import *
import sqlite3
app = Flask(__name__)
#Ensures the correct table is present before sending the form data
@app.route('/')
def main():
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
create_table_query = '''
CREATE TABLE IF NOT EXISTS voters (
VoterID INTEGER PRIMARY KEY AUTOINCREMENT,
VoterName TEXT
VoterAge INTEGER
CandidateID INTEGER REFERENCES candidates(CandidateID)
)'''
cursor.execute(create_table_query)
connection.commit()
connection.close()
return render_template('index.html')
# Pass the form data through app.py and to the database
@app.route('/', methods = ['POST'])
def send_to_database():
votername = request.form['name']
voterage = request.form['age']
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
query1 = "INSERT INTO voters (name,age) VALUES ('{n}','{a}')".format(n=votername,a=voterage)
cursor.execute(query1)
connection.commit()
if __name__== '__main__':
app.run(debug=True)
