日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RuntimeError: Working outside of request context. This typically means that you attempted to use fun

發(fā)布時間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RuntimeError: Working outside of request context. This typically means that you attempted to use fun 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

菜吃多了醉了

  • 一、報錯情況
  • 二、解決辦法

一、報錯情況

我在寫裝飾器的時候運行報錯:

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.

如圖:


我也是醉了,琢磨了半天。

https://blog.csdn.net/hanhanwanghaha寶藏女孩 歡迎您的關注!
歡迎關注微信公眾號:寶藏女孩的成長日記
讓這個可愛的寶藏女孩在努力的道路上與你一起同行!
如有轉載,請注明出處(如不注明,盜者必究)

二、解決辦法

裝飾器第一個參數(shù)是原函數(shù),如果裝飾器可以接收參數(shù)的話,那么后面可以跟別的參數(shù),否則就只有一個參數(shù)。所以,判斷裝飾器接收的參數(shù),如果只有一個并且第一個參數(shù)是可調用的,那么就是一個無參數(shù)的裝飾器(不需要加括號)。如果還有別的參數(shù),就返回一個生成裝飾器的函數(shù)。

在檢查代碼的時候我就發(fā)現(xiàn)了一個經(jīng)常犯的錯誤,就是不用加()


去掉括號就成功解決此問題

# coding:utf-8import time # 請求與響應 from flask import Flask, request, render_template# 1.初始化application app = Flask(__name__,template_folder="bbb",static_url_path="/ooo",static_folder="ooo")# 裝飾器 打印時間 def log_time(f):def decorator(*args, **kw):print(f'{time.time()}')return f(*args, **kw)return decorator# 2.添加路由 視圖函數(shù)(view function) @app.route("/login") @app.route("/cute") @app.route("/") @log_time def index():# 參數(shù)的獲取:littledata = request.argsname = littledata.get('username')print(name)# return "Hello,thi s is index!"# 響應為html格式# return "<p style ='color:green'>Hello,thi s is index!</p>"# 如果返回加載一個文件return render_template('login.html')# 3.運行服務器,如果不指定端口,就會默認端口5000 app.run(port=8003)

https://blog.csdn.net/hanhanwanghaha寶藏女孩 歡迎您的關注!
歡迎關注微信公眾號:寶藏女孩的成長日記
讓這個可愛的寶藏女孩在努力的道路上與你一起同行!
如有轉載,請注明出處(如不注明,盜者必究)

溫馨提示:千萬不能吃飽了就干代碼,休息會兒再敲,吃飽了會產(chǎn)生飯暈,你敲出來的不是代碼,而是bug!!! 哈哈哈 fighting@everyone

總結

以上是生活随笔為你收集整理的RuntimeError: Working outside of request context. This typically means that you attempted to use fun的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。