【发布时间】:2023-02-03 12:47:43
【问题描述】:
我在 VScode 中执行以下 python 脚本
可以提取mysql数据传给.html显示为网页内容
# get_sql_where_02.py
import mysql.connector
import webbrowser
import time
import pymysql
from flask import Flask,render_template,request
app = Flask(__name__)
mydb = mysql.connector.connect(
host="196.168.1.141",
user="Main_root",
password="password_123",
database="database_db",
auth_plugin='mysql_native_password'
)
mycursor = mydb.cursor()
# mycursor.execute("SELECT P_TITLE,P_DESC FROM webpage WHERE P_ID = 'en_1-01'")
mycursor.execute("SELECT P_TITLE,P_DESC FROM webpage WHERE P_ID = "+request.args["ProductID"])
myresult = mycursor.fetchall()
print(myresult) # does get the result I want
@app.route('/')
def index():
return render_template("index_test_0203.html", myresult = myresult)
if __name__ == "__main__":
app.run(debug=True)
- index_test_0203.html
<!DOCTYPE html>
<html>
<body>
<p> this is {{myresult}}</p>
</body>
</html>
- 错误信息
Traceback (most recent call last):
File "C:\Users\chuan\OneDrive\Desktop\custom_header_pra_1.12\test_showing_content_middle\get_sql_where_02.py", line 22, in <module>
mycursor.execute("SELECT P_TITLE,P_DESC FROM webpage WHERE P_ID = "+request.args["ProductID"])
File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\werkzeug\local.py", line 316, in __get__
obj = instance._get_current_object() # type: ignore[misc]
File "C:\Users\chuan\AppData\Local\Programs\Python\Python310\lib\site-packages\werkzeug\local.py", line 513, in _get_current_object
raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
我已经通读了讨论和教程,但不知道如何解决我的问题
【问题讨论】:
标签: python flask request runtime-error