More documentation updates :)
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 5144d3b · Browse files · 2 files·+129 added−0 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+129 added−0 removed
- other:+129 added−0 removed
Provenance
- Commit
0da39c7- Tree
43b2939- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5144d3b
2 files changed+129 added−0 removed
modified:docs/patterns/packages.rst+113 added
15 unmodified lines16 index.html17 login.html18 ...19 20+Simple Packages21+---------------22+23 To convert that into a larger one, just create a new folder24 `yourapplication` inside the existing one and move everything below it.25 Then rename `yourapplication.py` to `__init__.py`. (Make sure to delete26 all `.pyc` files first, otherwise things would most likely break)46 unmodified lines73 @app.route('/')74 def index():75 return 'Hello World!'76 77+You should then end up with something like that::78+79+ /yourapplication80+ /yourapplication81+ /__init__.py82+ /views.py83+ /static84+ /style.css85+ /templates86+ layout.html87+ index.html88+ login.html89+ ...90+91 .. admonition:: Circular Imports92 93 Every Python programmer hates them, and yet we just added some:94 circular imports (That's when two modules depend on each other. In this5 unmodified lines100 101 There are still some problems with that approach but if you want to use102 decorators there is no way around that. Check out the103 :ref:`becomingbig` section for some inspiration how to deal with that.104+105+106+.. _working-with-modules:107+108+Working with Modules109+--------------------110+111+For larger applications with more than a dozen views it makes sense to112+split the views into module. First let's look at the typical struture 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 method,137+but most of them are the same.138+139+Long story short, here 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 login():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+ from yourapplication.views.admin import admin162+ from yourapplication.views.frontend import frontend163+164+ app = Flask(__name__)165+ app.register_module(admin)166+ app.register_module(frontend)167+168+So what is different when working with modules? It mainly affects URL169+generation. Remember the :func:`~flask.url_for` function? When not170+working with modules it accepts the name of the function as first171+argument. This first argument is called the "endpoint". When you are172+working with modules you can use the name of the function like you did173+without, when generating modules from a function or template in the same174+module. If you want to generate the URL to another module, prefix it with175+the name of the module and a dot.176+177+Confused? Let's clear that up with some examples. Imagine you have a178+method in one module (say `admin`) and you want to redirect to a179+different module (say `frontend`). This would look like this::180+181+ @admin.route('/to_frontend')182+ def to_frontend():183+ return redirect(url_for('frontend.index'))184+185+ @frontend.route('/')186+ def index():187+ return "I'm the frontend index"188+189+Now let's say we only want to redirect to a different module in the same190+module. Then we can either use the full qualified endpoint name like we191+did in the example above, or we just use the function name::192+193+ @frontend.route('/to_index')194+ def to_index():195+ return redirect(url_for('index'))196+197+ @frontend.route('/')198+ def index():199+ return "I'm the index"modified:flask.py+16 added
141 unmodified lines142 143 144 def url_for(endpoint, **values):145 """Generates a URL to the given endpoint with the method provided.146+ The endpoint is relative to the active module if modules are in use.147 148+ Here some examples:149+150+ ==================== ======================= =============================151+ Active Module Target Endpoint Target Function152+ ==================== ======================= =============================153+ `None` ``'index'`` `index` of the application154+ `None` ``'.index'`` `index` of the application155+ ``'admin'`` ``'index'`` `index` of the `admin` module156+ any ``'.index'`` `index` of the application157+ any ``'admin.index'`` `index` of the `admin` module158+ ==================== ======================= =============================159+160 :param endpoint: the endpoint of the URL (name of the function)161 :param values: the variable arguments of the URL rule162 """163 ctx = _request_ctx_stack.top230 unmodified lines394 395 @admin.route('/login')396 def login():397 pass398+399+ For a gentle introduction into modules, checkout the400+ :ref:`working-with-modules` section.401 """402 403 def __init__(self, import_name, name=None, url_prefix=None):404 if name is None:591 unmodified lines