Search code examples
pythonjinja2blueprint

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


I am using python 3.9.6 with ninja2 and blueprint.

py --version

returns:

Python 3.9.6

As far as I know, blueprint is a seperate folder, with seperate html templates and static files (css/js) , and I can have many blueprints in one runnable python project.

I have looked at Views with Jinja2 and blueprint

The hierarchy of the html+relevant files of myblueprint is:

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

The relevant code:

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)

For some reason, when I run: https://localhost:5000, I get an error:

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 -

I have also noticed that on the chrome network console (not in code run window), I see another exception:

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

What is the cross origin for blueprint, and how can I avoid that?

What is wrong in my code above, and should I fix the heirarchy?


Solution

  • Two problems in your code. First you have to register your blueprint

    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)
    

    Then your html file must simply be in the templates folder:

    > templates > myblueprint.html