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
Provenance
- Commit
dbfa04e- Tree
11a8f9d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
62d91d6
2 files changed+23 added−181 removed
modified:docs/patterns/packages.rst+6 added-181 removed
103 unmodified lines104 105 106 .. _working-with-modules:107 108-Working with Modules109---------------------108+Working with Blueprints109+-----------------------110 111-For larger applications with more than a dozen views it makes sense to112-split the views into modules. First let's look at the typical structure of113-such an application::114-115- /yourapplication116- /yourapplication117- /__init__.py118- /views119- __init__.py120- admin.py121- frontend.py122- /static123- /style.css124- /templates125- layout.html126- index.html127- login.html128- ...129-130-The views are stored in the `yourapplication.views` package. Just make131-sure to place an empty `__init__.py` file in there. Let's start with the132-`admin.py` file in the view package.133-134-First we have to create a :class:`~flask.Module` object with the name of135-the package. This works very similar to the :class:`~flask.Flask` object136-you have already worked with, it just does not support all of the methods,137-but most of them are the same.138-139-Long story short, here's a nice and concise example::140-141- from flask import Module142-143- admin = Module(__name__)144-145- @admin.route('/')146- def index():147- pass148-149- @admin.route('/login')150- def login():151- pass152-153- @admin.route('/logout')154- def logout():155- pass156-157-Do the same with the `frontend.py` and then make sure to register the158-modules in the application (`__init__.py`) like this::159-160- from flask import Flask161- app = Flask(__name__)162-163- from yourapplication.views.admin import admin164- from yourapplication.views.frontend import frontend165- app.register_module(admin, url_prefix='/admin')166- app.register_module(frontend)167-168-We register the modules with the app so that it can add them to the169-URL map for our application. Note the prefix argument to the admin170-module: by default when we register a module, that module's end-points171-will be registered on `/` unless we specify this argument.172-173-So what is different when working with modules? It mainly affects URL174-generation. Remember the :func:`~flask.url_for` function? When not175-working with modules it accepts the name of the function as first176-argument. This first argument is called the "endpoint". When you are177-working with modules you can use the name of the function like you did178-without, when generating modules from a function or template in the same179-module. If you want to generate the URL to another module, prefix it with180-the name of the module and a dot.181-182-Confused? Let's clear that up with some examples. Imagine you have a183-method in one module (say `admin`) and you want to redirect to a184-different module (say `frontend`). This would look like this::185-186- @admin.route('/to_frontend')187- def to_frontend():188- return redirect(url_for('frontend.index'))189-190- @frontend.route('/')191- def index():192- return "I'm the frontend index"193-194-Now let's say we only want to redirect to a different function in the same195-module. Then we can either use the full qualified endpoint name like we196-did in the example above, or we just use the function name::197-198- @frontend.route('/to_index')199- def to_index():200- return redirect(url_for('index'))201-202- @frontend.route('/')203- def index():204- return "I'm the index"205-206-.. _modules-and-resources:207-208-Modules and Resources209----------------------210-211-.. versionadded:: 0.5212-213-If a module is located inside an actual Python package it may contain214-static files and templates. Imagine you have an application like this::215-216-217- /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- ...239-240-The static folders automatically become exposed as URLs. For example if241-the `admin` module is exported with an URL prefix of ``/admin`` you can242-access the style css from its static folder by going to243-``/admin/static/style.css``. The URL endpoint for the static files of the244-admin would be ``'admin.static'``, similar to how you refer to the regular245-static folder of the whole application as ``'static'``.246-247-If you want to refer to the templates you just have to prefix it with the248-name of the module. So for the admin it would be249-``render_template('admin/list_items.html')`` and so on. It is not250-possible to refer to templates without the prefixed module name. This is251-explicit unlike URL rules. Also with the move of the views into from252-`yourapplication.views.admin` too `yourapplication.apps.admin.views` you253-will have to give the module an explit shortname. Why? Because otherwise254-all your modules will be internally known as `views` which is obviously255-not what you want::256-257- # in yourapplication/apps/admin/views.py258- admin = Module(__name__, 'admin')259-260-The setup code changes slightly because of the imports::261-262- # in yourapplication/__init__.py263- from flask import Flask264- from yourapplication.apps.admin.views import admin265- from yourapplication.apps.frontend.views import frontend266-267- app = Flask(__name__)268- app.register_module(admin, url_prefix='/admin')269- app.register_module(frontend)270-271-.. admonition:: References to Static Folders272-273- 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.276-277- If you want to refer to the application's static folder, use a leading278- dot::279-280- # this refers to the application's static folder281- url_for('.static', filename='static.css')282-283- # this refers to the current module's static folder284- url_for('static', filename='static.css')285-286- 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.111+If you have larger applications it's recommended to divide them into112+smaller groups where each group is implemented with the help of a113+blueprint. For a gentle introduction into this topic refer to the114+:ref:`blueprints` chapter of the documentation.modified:docs/blueprints.rst+17 added
66 unmodified lines67 Additionally it will prefix the endpoint of the function with the68 name of the blueprint which was given to the :class:`Blueprint`69 constructor (in this case also ``simple_page``).70 71+Registering Blueprints72+----------------------73+74 So how do you register that blueprint? Like this::75 76 from flask import Flask77 from yourapplication.simple_page import simple_page12 unmodified lines90 files. The other two are for the `show` function of the ``simple_page``91 blueprint. As you can see, they are also prefixed with the name of the92 blueprint and separated by a dot (``.``).93 94+Blueprints however can also be mounted at different locations::95+96+ app.register_blueprint(simple_page, url_prefix='/pages')97+98+And sure enough, these are the generated rules::99+100+ [<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>]103+104+On top of that you can register blueprints multiple times though not every105+blueprint might respond properly to that. In fact it depends on how the106+blueprint is implemented if it can be mounted more than once.107+108 Blueprint Resources109 -------------------110 111 Blueprints can provide resources as well. Sometimes you might want to82 unmodified lines