Skip to content
Myr

Removed old module documentation and added some new for blueprints.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 62d91d6 · Browse files · 2 files+23 added−181 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+23 added−181 removed

  • other:+23 added−181 removed

2 files+23 added−181 removed

modified:docs/patterns/packages.rst+6 added-181 removed
103 unmodified lines
104105106.. _working-with-modules:107108Working with Modules109--------------------108Working with Blueprints109-----------------------110111For larger applications with more than a dozen views it makes sense to112split the views into modules. First let's look at the typical structure of113such an application::114115 /yourapplication116 /yourapplication117 /__init__.py118 /views119 __init__.py120 admin.py121 frontend.py122 /static123 /style.css124 /templates125 layout.html126 index.html127 login.html128 ...129130The views are stored in the `yourapplication.views` package. Just make131sure to place an empty `__init__.py` file in there. Let's start with the132`admin.py` file in the view package.133134First we have to create a :class:`~flask.Module` object with the name of135the package. This works very similar to the :class:`~flask.Flask` object136you have already worked with, it just does not support all of the methods,137but most of them are the same.138139Long story short, here's a nice and concise example::140141 from flask import Module142143 admin = Module(__name__)144145 @admin.route('/')146 def index():147 pass148149 @admin.route('/login')150 def login():151 pass152153 @admin.route('/logout')154 def logout():155 pass156157Do the same with the `frontend.py` and then make sure to register the158modules in the application (`__init__.py`) like this::159160 from flask import Flask161 app = Flask(__name__)162163 from yourapplication.views.admin import admin164 from yourapplication.views.frontend import frontend165 app.register_module(admin, url_prefix='/admin')166 app.register_module(frontend)167168We register the modules with the app so that it can add them to the169URL map for our application. Note the prefix argument to the admin170module: by default when we register a module, that module's end-points171will be registered on `/` unless we specify this argument.172173So what is different when working with modules? It mainly affects URL174generation. Remember the :func:`~flask.url_for` function? When not175working with modules it accepts the name of the function as first176argument. This first argument is called the "endpoint". When you are177working with modules you can use the name of the function like you did178without, when generating modules from a function or template in the same179module. If you want to generate the URL to another module, prefix it with180the name of the module and a dot.181182Confused? Let's clear that up with some examples. Imagine you have a183method in one module (say `admin`) and you want to redirect to a184different module (say `frontend`). This would look like this::185186 @admin.route('/to_frontend')187 def to_frontend():188 return redirect(url_for('frontend.index'))189190 @frontend.route('/')191 def index():192 return "I'm the frontend index"193194Now let's say we only want to redirect to a different function in the same195module. Then we can either use the full qualified endpoint name like we196did in the example above, or we just use the function name::197198 @frontend.route('/to_index')199 def to_index():200 return redirect(url_for('index'))201202 @frontend.route('/')203 def index():204 return "I'm the index"205206.. _modules-and-resources:207208Modules and Resources209---------------------210211.. versionadded:: 0.5212213If a module is located inside an actual Python package it may contain214static files and templates. Imagine you have an application like this::215216217 /yourapplication218 __init__.py219 /apps220 __init__.py221 /frontend222 __init__.py223 views.py224 /static225 style.css226 /templates227 index.html228 about.html229 ...230 /admin231 __init__.py232 views.py233 /static234 style.css235 /templates236 list_items.html237 show_item.html238 ...239240The static folders automatically become exposed as URLs. For example if241the `admin` module is exported with an URL prefix of ``/admin`` you can242access the style css from its static folder by going to243``/admin/static/style.css``. The URL endpoint for the static files of the244admin would be ``'admin.static'``, similar to how you refer to the regular245static folder of the whole application as ``'static'``.246247If you want to refer to the templates you just have to prefix it with the248name of the module. So for the admin it would be249``render_template('admin/list_items.html')`` and so on. It is not250possible to refer to templates without the prefixed module name. This is251explicit unlike URL rules. Also with the move of the views into from252`yourapplication.views.admin` too `yourapplication.apps.admin.views` you253will have to give the module an explit shortname. Why? Because otherwise254all your modules will be internally known as `views` which is obviously255not what you want::256257 # in yourapplication/apps/admin/views.py258 admin = Module(__name__, 'admin')259260The setup code changes slightly because of the imports::261262 # in yourapplication/__init__.py263 from flask import Flask264 from yourapplication.apps.admin.views import admin265 from yourapplication.apps.frontend.views import frontend266267 app = Flask(__name__)268 app.register_module(admin, url_prefix='/admin')269 app.register_module(frontend)270271.. admonition:: References to Static Folders272273 Please keep in mind that if you are using unqualified endpoints by274 default Flask will always assume the module's static folder, even if275 there is no such folder.276277 If you want to refer to the application's static folder, use a leading278 dot::279280 # this refers to the application's static folder281 url_for('.static', filename='static.css')282283 # this refers to the current module's static folder284 url_for('static', filename='static.css')285286 This is the case for all endpoints, not just static folders, but for287 static folders it's more common that you will stumble upon this because288 most applications will have a static folder in the application and not289 a specific module.111If you have larger applications it's recommended to divide them into112smaller groups where each group is implemented with the help of a113blueprint. For a gentle introduction into this topic refer to the114:ref:`blueprints` chapter of the documentation.
modified:docs/blueprints.rst+17 added
66 unmodified lines
67Additionally it will prefix the endpoint of the function with the68name of the blueprint which was given to the :class:`Blueprint`69constructor (in this case also ``simple_page``).7071Registering Blueprints72----------------------7374So how do you register that blueprint? Like this::7576 from flask import Flask77 from yourapplication.simple_page import simple_page
12 unmodified lines
90files. The other two are for the `show` function of the ``simple_page``91blueprint. As you can see, they are also prefixed with the name of the92blueprint and separated by a dot (``.``).9394Blueprints however can also be mounted at different locations::9596 app.register_blueprint(simple_page, url_prefix='/pages')9798And sure enough, these are the generated rules::99100 [<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>,101 <Rule '/pages/<page>' (HEAD, OPTIONS, GET) -> simple_page.show>,102 <Rule '/pages/' (HEAD, OPTIONS, GET) -> simple_page.show>]103104On top of that you can register blueprints multiple times though not every105blueprint might respond properly to that. In fact it depends on how the106blueprint is implemented if it can be mounted more than once.107108Blueprint Resources109-------------------110111Blueprints can provide resources as well. Sometimes you might want to
82 unmodified lines