[FIXED] Blueprint error in Flask: NameError: name of blueprint is not defined

Issue

I am just getting started with Flask and have been working on a very simple site to get acquainted with its main features before adding other stuff on and I’m getting, what seems to me, an inscrutable problem. I’ve looked at numerous tutorials and have no idea where I’m going wrong. My site directory is very simple:

home/user/website_name
├── ock/
│   ├── __init__.py
│   ├── .env
│   ├── routes.py
│   ├── templates/
│   │   ├── base.html
│   │   └── home.html
│   └── static/
│       └── style.css
├── instance/
│   └── config.py
├── venv/
├── setup.py
└── wsgi.py

__init__.py is here:

from flask import Flask, render_template

def make_app():
    app = Flask(__name__, instance_relative_config=True) 
    app.config.from_pyfile('config.py')
    
    from . import routes
    app.register_blueprint(pages)

routes.py is here:

from flask import render_template, Blueprint
from wsgi import app


bp = Blueprint(
    'pages', __name__, 
    template_folder='templates', 
    static_folder='static'
)


@app.route('/')
@app.route('/index')
@app.route('/home')
def home():
    return render_template(
        'home.html', 
    )

When I try to go to http://127.0.0.1:5000/home, it spit out: NameError: name ‘pages’ is not defined.

Edit to clarify question: How to do I get the blueprint to be recognized by Flask?

Solution

I figured out how to make it work. All I did was modify app.register_blueprint(pages) to app.register_blueprint(routes.pages). I guess that told Flask where to look for the blueprint?

So much left to learn.

Answered By – jprado

Answer Checked By – Timothy Miller (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published