【问题标题】:404 error when trying to display the sql table content in the HTML webpage using Python flask尝试使用 Python flask 在 HTML 网页中显示 sql 表内容时出现 404 错误
【发布时间】: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,请检查您的拼写并重试。

标签: python html mysql flask


【解决方案1】:

您的显示路由仅适用于 POST 请求,但您的浏览器会发送 get 请求。

@app.route('/display',methods=['POST'])
def display():

要么将POST 更改为GET,要么完全删除methods=...,因为GET 是默认值。

如果这听起来令人困惑,请查看以下解释 HTTP 动词的文章

https://developer.mozilla.org/de/docs/Web/HTTP/Methods

更新

您必须将代码更改为任一

@app.route('/display',methods=['GET'])
def display():

@app.route('/display')
def display():

【讨论】:

  • 你好,朋友,谢谢你的回答。我已将该方法更改为 POST 并尝试显示,但下次我完全删除方法时发生了同样的错误,但那次我也得到了相同的错误 404,确切的错误是,127.0.0.1 - - [06/Feb/2021 19 :28:26]“[33mGET /display.html HTTP/1.1[0m”404 -
  • 网页中的错误是,未找到服务器上未找到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。
  • 我会更新我的答案 - 并希望让它更清晰。
  • 感谢您的帮助。我发现了错误。在 index.html 内 &lt;div class="display"&gt; &lt;a href="display.html" method="POST"&gt; &lt;button&gt;Display records&lt;/button&gt; &lt;/a&gt; &lt;/div&gt; 它应该作为 display 而不是在标签内的 display.html 给出 ``` ``` 应​​该是 ``` ``` 喜欢这样 希望这个有用。
  • 好的,你还有一个问题。不过,我的回答是有效的。您应该将路由更改为 GET 并从链接中删除 method="POST"
猜你喜欢
  • 2021-03-30
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多