I'm working on an app using Blueprints and I followed the documentation quickstart on Blueprints, but still have a 404 on my templates.
I got an admin blueprint looking like that:
from flask import Blueprint, render_template
admin = Blueprint('admin', __name__, template_folder='templates')
#admin.route('/')
def admin_index():
return render_template('admin/index.html.jinja2')
As described in the doc, my template is in /myapp/admin/templates/index.html.jinja2+
I register the blueprint in my app __init__.py file:
from flask import Flask
from . import config
from admin import admin
app = Flask(__name__)
app.config.from_object(config.DevelopmentConfig)
# register my blueprint
app.register_blueprint(admin, url_prefix='/admin/')
If anyone has an idea about a mistake I could have done, please tell me!
You are not accurate about docs. Your templates shoud be in
myapp/admin/templates/admin/index.html.jinja2
Note admin folder in templates.
Related
I'm currently working on learning Flask and create a working page with login functions. Now I want to remove the global app instance and started using blueprints for the submodules.
My project is structured like this:
+ app
+ auth
- __init__.py
- forms.py
- routes.py
+ main
+ models
+ templates
- __init__.py
+ migrations
- index.py
- config.py
No I added a blueprint to the routes.py and used the decorators there:
from flask import render_template, flash, redirect, url_for, request, Blueprint
from app import app, db
from app.auth.forms import LoginForm, RegistrationForm
# ...
from app.models.User import User
blueprint = Blueprint('auth', __name__)
#blueprint.route('/login', methods=['GET', 'POST'])
def login():
return "example login"
The __init__.py of the auth module:
from . import forms, routes
The blueprint gets added in the __init__.py of the app folder:
# ...
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
from app.auth.routes import blueprint as auth_bp
app.register_blueprint(auth_bp)
After using the #blueprint decorators, I don't need #app anymore, but how can I access the db when I want to remove the import app and the from app.models.User import User part?
from . import db
from ..models.User import User
There are two things to understand here. app as a module (the folder) and app the instance of flask inside __init__.py. When you do import app inside authentication blueprint then you are actually importing the whole app module and not the flask instance. When you do from app import app you are actually importing flask instance from app module. This can be confusing to eliminate this I advise you to change the name of app folder to something different like bacher then when you need to import db inside your authentication blueprint use from bacher import db and for User model from bacher.models.User import User
I'm a bit confused by the changes to Flask 1.0.
The docs suggest creating a factory for the application in __init__.py where app=create_app(config_file) which is called from run.py.
Now in prior versions of Flask I had could from application import app in say a blueprint controller because app was not returned from create_app(). I can't call create_app() again, yet I don't have access to #app.route, app.config etc, etc.
I've tried and had little success pushing the context. Now there are various not-so-nice ways of doing this but what is the "canonical" way?
The concept of an application factory is not new to Flask 1.0. You don't have to use a factory if your app doesn't need one.
If you do use an app factory, then you usually do not register any routes directly on the app. You'd create a blueprint instead, register your routes to the blueprint, and in the app factory, attach the blueprint to the app.
Anywhere you need to access the app configuration, you'd use the current_app proxy.
For example, in the views.py module in your application you could create a blueprint just for the views; I included an example use of current_app to access configuration:
from flask import Blueprint, current_app
bp = BluePrint('main', __name__)
#bp.route(...)
def some_route_name(...):
# ...
if current_app.config['SOME_CONFIGURATION_SETTING']:
# ...
and you'd register the blueprint with
def create_app(config_filename=None):
app = Flask(__name__)
# configuration setup, use the confuration module as a default
from . import configuration
app.config.from_object(config)
app.config.from_envvar("PROJECTNAME_SETTINGS", silent=True)
if config_filename is not None:
app.config.from_pyfile(config_filename)
from . import views
app.register_blueprint(views.bp)
# other registrations
return app
I am taking over a Flask application that has a user module, but does not have a landing page. The structure of the project is:
|-application.py
|-manage.py
|-settings.py
|-/templates
|----base.html
|----index.html
|----navbar.html
|----/user
|--------views.py
application.py:
from flask import Flask
....
def create_app(**config_overrides):
app = Flask(__name__)
app.config.from_pyfile('settings.py')
app.config.update(config_overrides)
db.init_app(app)
from user.views import user_app
app.register_blueprint(user_app)
return app
user/views.py:
from flask import Blueprint, render_template, request, redirect, session, url_for, abort
...
user_app = Blueprint('user_app', __name__)
#user_app.route('login', methods = ('GET','POST'))
def login():
....
I placed index.html in the templates folder.
Should I place a view.py in the root directory where I would put a route to an index.html?
You can add additional routes anywhere you want.
However, since the package uses a create_app() app factory, you can't register those routes with an #app.route() decorator; the app is not created in a way you can just import.
So yes, creating a views.py module is a good organisational idea, but do create a Blueprint() there too, and register that blueprint in the create_app() function with the Flask() app instance.
In views.py:
from flask import Blueprint
bp = Blueprint('main', __name__)
#main.route('/')
def index():
# produce a landing page
and in create_app() in application.py, add
import views
app.register_blueprint(views.bp)
(I use a convention of using the bp variable name for all my blueprints, and then just importing the module and registering the module.bp attribute).
It's the explicit import and app.register_blueprint() call that ties any of the blueprints used in a Flask project into the final app's routes. Blueprints can share the same prefix, including having no prefix.
One thing to note: here the views module is now a top-level module next to application.py. It'd be better if everything was moved into a package, giving you more freedom over what names you use. All modules in a single package are namespaced, kept separate from other top-level modules such as json and time and flask, so there won't be clashes when you want to use the same name for one of the additional modules in your project.
You'd move everything but manage.py into a subdirectory, with a project-appropriate name, and move application.py to __init__.py. Imports can then use from . import ... to import from the current package, etc.
I'm learning Blueprints for Flask, but I am having trouble with importing the correct modules. This is my setup:
Folder structure:
- app.py
templates/
nomad/
- __init__.py
- nomad.py
app.py
from flask import Flask
from nomad.nomad import nblueprint
app = Flask(__name__)
app.register_blueprint(nblueprint)
nomad.py
from flask import render_template, Blueprint, abort
from app import app
nblueprint = Blueprint('nblueprint', __name__, template_folder='templates')
# Routes for this blueprint
#app.route ....
__init__.py is empty
The error I'm getting: ImportError: cannot import name nblueprint. I know my import statement is probably wrong, but what should it be and why?
EDIT:
If I remove from app import app, then I can successfully import nblueprint in app.py. But I need app in nomad.py because it needs to handle routes. Why is that line causing issues with importing, and how would I get around this?
Blueprints is for define application route so you don't need to use app instance and blueprint in same place for route defination.
#nomad.py
#nblueprint.route('/')
You are getting error because while you register the blueprint at the same time you use app instance. So as you said when you remove the from app ... it solve the problem.
The recommend way is define your view for that blueprint in blueprint package in your example nomad package, it should be like this:
...
nomad/
__init__.py
views.py
#nomad/__init__.py
nblueprint = Blueprint(...)
#nomad/views.py
from . import nblueprint
#nblueprint.route('/')
...
This is my application structure:
/blog
/blog
/app.py
models.py
views.py
/admin
__init__py
views.py
...
I want to use flask-admin extension in a different package.
in /admin/__init__.py I imported the app and flask-admin extension:
from flask.ext.admin import Admin
from app import app
then I initiate the admin app like that:
admin = Admin(app)
However, I get 404 error. Why? Should I use blueprint or what?
I assume you're trying to hit the default /admin routes within your Flask app for Flask admin?
My guess right now is that none of your code does import admin anywhere, which is probably good since admin's __init__.py will try to re-import your app.py all over again (from the from app import app reference) and you'll end up in a circular dependency.
What I'd do is alter app.py to contain the admin = Admin(app) and from flask.ext.admin import Admin code, and also do a from admin import views and empty out the admin/__init__.py file completely.