【问题标题】:Flask request with RuntimeError: Working outside of request context带有 RuntimeError 的 Flask 请求:在请求上下文之外工作
【发布时间】: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


    【解决方案1】:

    你不能在那里使用请求,因为mycursor.execute("SELECT P_TITLE,P_DESC FROM webpage WHERE P_ID = "+request.args["ProductID"])行是在脚本启动时执行的。但是您无法在那里访问请求。

    您可以在收到任何请求后访问请求。

    像这样使用

    @app.route('/')
    def index():
        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 
    
        return render_template("index_test_0203.html", myresult = myresult)
    

    并从上面的地方删除这些行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      相关资源
      最近更新 更多