python - 蓝图声明 运行 有例外,因为我从浏览器导航它

python - blueprint declaration run with exceptions as I navigate it from a browser

我正在使用带有 ninja2 和蓝图的 python 3.9.6。

py --version

returns:

Python 3.9.6

据我所知,blueprint 是一个单独的文件夹,有单独的 html 模板和静态文件 (css/js),我可以在一个 运行nable 中包含多个蓝图python 项目。

我看过Views with Jinja2 and blueprint

myblueprint的html+相关文件的层级是:

main_project -> myblueprint -> templates -> myblueprint.html -> static -> myblueprint.css -> myblueprint.js

相关代码:

import os
from flask import Flask, Blueprint, redirect, request, render_template, url_for, session
main_page = Blueprint('myblueprint', __name__)


@main_page.route("/myblueprint")
def home():
    query = 'select * from users;'
    users = interact_db(query=query, query_type='fetch')
    return render_template('myblueprint.html')
    
...
@app.route('/', methods=['GET'])
def main():
    return redirect("myblueprint")


if __name__ == '__main__':
    #   Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.secret_key = '12345'
    app.run(host='0.0.0.0', port=port)

出于某种原因,当我 运行: https://localhost:5000 时,出现错误:

ERROR in app: Exception on /myblueprint [GET] Traceback (most recent call last): File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 2073, in wsgi_app response = self.full_dispatch_request() File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1516, in full_dispatch_request rv = self.dispatch_request() File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "C:/main_project/myproject/app.py", line 17, in home return render_template('myblueprint.html') File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\templating.py", line 148, in render_template ctx.app.jinja_env.get_or_select_template(template_name_or_list), File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\environment.py", line 1068, in get_or_select_template return self.get_template(template_name_or_list, parent, globals) File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\environment.py", line 997, in get_template return self._load_template(name, globals) File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\environment.py", line 958, in _load_template template = self.loader.load(self, name, self.make_globals(globals)) File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jinja2\loaders.py", line 125, in load source, filename, uptodate = self.get_source(environment, name) File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\templating.py", line 59, in get_source return self._get_source_fast(environment, template) File "C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\lib\site-packages\flask\templating.py", line 95, in _get_source_fast raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: myblueprint.html 127.0.0.1 - - [04/Jan/2022 08:04:31] "GET /myblueprint HTTP/1.1" 500 - 127.0.0.1 - - [04/Jan/2022 08:04:31] "GET /favicon.ico HTTP/1.1" 404 -

我还注意到在 chrome 网络控制台上(不是在代码 运行 window 中),我看到另一个异常:

Request URL: http://localhost:5000/
Request Method: GET
Status Code: 500 INTERNAL SERVER ERROR
Remote Address: 127.0.0.1:5000
Referrer Policy: strict-origin-when-cross-origin

什么是蓝图的交叉原点,我该如何避免?

我上面的代码有什么问题,我应该修复层次结构吗?

您的代码中有两个问题。首先你必须注册你的蓝图

import os
from flask import Flask, Blueprint, redirect, request, render_template, url_for, session

app = Flask(__name__)
main_page = Blueprint('myblueprint', __name__)


@main_page.route('/myblueprint', methods=['GET'])
def home():
    query = 'select * from users;'
    users = interact_db(query=query, query_type='fetch')
    return render_template('myblueprint.html')


@app.route('/', methods=['GET'])
def main():
    return redirect("myblueprint")


app.register_blueprint(main_page)

if __name__ == '__main__':
    #   Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5800))
    app.secret_key = '12345'
    app.run(host='0.0.0.0', port=port)

那么您的 html 文件必须位于模板文件夹中:

> templates > myblueprint.html