【发布时间】:2021-05-10 01:11:11
【问题描述】:
我创建了一个简单的 BMI 计算应用程序,该代码用于将记录插入表中,但我无法显示记录。
这是我的代码。请帮我解决这个问题。 此代码成功将记录插入到表中。但未能显示到网页。
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>
请帮我解决这个问题。
【问题讨论】:
-
请显示完整的错误信息。
-
未找到 在服务器上未找到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。