我正在嘗試制作一個燒瓶網站,到目前為止它正在努力列印一個你好世界。當我嘗試從輸出文本轉換為輸出簡單的 html 模板時,站點中斷并出現以下錯誤。
這是我的代碼:
from flask import render_template, Blueprint
recipes_blueprint = Blueprint('recipes', __name__, template_folder='templates')
@recipes_blueprint.route('/')
def index():
return
render_template('list.html')
我的 list.html 只是模板檔案夾中的以下內容:
<h1>Test</h1>
完整的錯誤在這里:
2021-10-16 07:50:53,399: from app import app as application # noqa
2021-10-16 07:50:53,399:
2021-10-16 07:50:53,399: File "/home/jameshiven/mysite/app.py", line 3, in <module>
2021-10-16 07:50:53,399: from products.views import products_bp
2021-10-16 07:50:53,399:
2021-10-16 07:50:53,399: File "/home/jameshiven/mysite/products/views.py", line 12, in <module>
2021-10-16 07:50:53,399: render_template('list.html')
2021-10-16 07:50:53,399:
2021-10-16 07:50:53,399: File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 146, in render_template
2021-10-16 07:50:53,399: ctx.app.update_template_context(context)
2021-10-16 07:50:53,400:
uj5u.com熱心網友回復:
藍圖應該在燒瓶應用程式中注冊才能出現在應用程式中。
將您的代碼修改為
from flask import render_template, Blueprint, Flask
recipes_blueprint = Blueprint('recipes', __name__, template_folder='templates')
app = Flask(__name__)
app.register_blueprint(recipes_blueprint)
@app.route('/')
def index():
return render_template('list.html')
if __name__ == '__main__':
app.run()
這里有一些例子,這里
uj5u.com熱心網友回復:
您沒有提供實際的例外訊息,但我猜它是:
AttributeError: 'NoneType' object has no attribute 'app'
這是因為您的視圖功能搞砸了。改成這樣:
@recipes_blueprint.route('/')
def index():
return render_template('list.html')
然后app像約翰建議的那樣使用物件注冊您的藍圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/318863.html
上一篇:并發和JPA存盤庫行為
下一篇:使用回圈創建Flask端點
