I have created a simple BMI calculation application the code works for inserting records into a table but I can't able to display the records.
Here is my code. kindly, help me out to solve this issue.
this code successfully inserted records to table.but failed to display to the webpage.
app.py
from flask import Flask,render_template,request,redirect,url_for,flash
from flask_mysqldb import MySQL
app=Flask(__name__)
app.secret_key="flash message"
app.config['MYSQL_HOST']='localhost'
app.config['MYSQL_USER']='root'
app.config['MYSQL_PASSWORD']='mypassword'
app.config['MYSQL_DB']='bmicalculate'
mysql=MySQL(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/display',methods=['POST'])
def display():
cur=mysql.connection.cursor()
cur.execute("select * from userdetails")
data=cur.fetchall()
cur.close()
return render_template('display.html',data=data)
@app.route('/insert',methods=['POST'])
def insert():
if request.method=="POST":
name=request.form['name']
age=request.form['age']
weight=request.form['weight']
height=request.form['height']
weight=int(weight)
height=int(height)
bmi=((weight)/(height*height))*10000
bmi=int(bmi)
cur=mysql.connection.cursor()
cur.execute("insert into userdetails(name,age,weight,height,bmi) values (%s,%s,%s,%s,%s)",(name,age,weight,height,bmi))
mysql.connection.commit()
return redirect(url_for('index'))
if __name__=="__main__":
app.run(debug=True
index.html
<!DOCTYPE html>
<html>
<head>
<title>BMI Application</title>
<link rel='stylesheet' href="{{url_for('static',filename='style.css')}}">
</head>
<body>
<div class='container'>
<h1>BMI Calculation Web Development</h1>
</div>
<br><br>
<div class="display">
<a href="display.html" method="POST">
<button>Display records</button>
</a>
</div>
<div class="calculate">
<h2>Enter your details</h2>
<form action="{{url_for('insert')}}" method="POST">
<div class="form">
<label>Name:</label>
<input type="text" class="form" name="name" placeholder="enter your name" required="1"><br><br>
</div>
<div class="form">
<label>Age:</label>
<input type="text" class="form" name="age" placeholder="enter your age" required="1"><br><br>
</div>
<div class="form">
<label>Weight:</label>
<input type="text" class="form" name="weight" placeholder="enter your weight in kgs" required="1"><br><br>
</div>
<div class="form">
<label>Height:</label>
<input type="text" class="form" name="height" placeholder="enter your height in cm" required="1"><br><br>
</div>
<div class="form">
<button class="btn" type="submit">Submit</button><br><br>
</div>
</form>
</div>
</body>
</html>
display.html
<!DOCTYPE html>
<html>
<head>
<title>User Records</title>
</head>
<body>
<style>
td {
width: 150px;
color:blueviolet;
text-align: center;
border: 1px solid black;
padding: 5px;
}
</style>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Weight</th>
<th>Height</th>
<th>BMI</th>
</tr>
</thead>
<tbody>
{% for row in data %}
<tr>
<td>{{row[0]}}</td>
<td>{{row[1]}}</td>
<td>{{row[2]}}</td>
<td>{{row[3]}}</td>
<td>{{row[4]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
Please help me out to solve this issue.
question from:
https://stackoverflow.com/questions/66067581/404-error-when-trying-to-display-the-sql-table-content-in-the-html-webpage-using 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…