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)
You're using an incorrect value for the form's action
attribute. Your defined route for the form data is accessible at /
, so you should also use this path as the value.
<form style="text-align: center;" method="POST" action="/">
<!-- ... -->
</form>
Another option is to use url_for()
. This function accepts the endpoint identifier and automatically generates the corresponding URL.
<form style="text-align: center;" method="POST" action="{{ url_for('send_to_database') }}">
<!-- ... -->
</form>
Also note that the route that accepts the form data currently has no return value. This will result in another error message. You can therefore either display something, for example, with render_template()
, or redirect to another endpoint with redirect()
.
@app.route('/', methods = ['POST'])
def send_to_database():
# ...
return redirect(url_for('main'))
I recommend taking a look at some Flask tutorials. The Flask documentation includes a small sample application called "Flaskr", and the Flask Mega Tutorial is also always worth a look. You can also find a list of worthwhile resources on the Flask wiki page on SO.
Have fun with your project.