Skip to content
Myr

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

2 files+129 added−0 removed

modified:docs/patterns/packages.rst+113 added
15 unmodified lines
16 index.html17 login.html18 ...1920Simple Packages21---------------2223To convert that into a larger one, just create a new folder24`yourapplication` inside the existing one and move everything below it.25Then rename `yourapplication.py` to `__init__.py`. (Make sure to delete26all `.pyc` files first, otherwise things would most likely break)
46 unmodified lines
73 @app.route('/')74 def index():75 return 'Hello World!'7677You should then end up with something like that::7879 /yourapplication80 /yourapplication81 /__init__.py82 /views.py83 /static84 /style.css85 /templates86 layout.html87 index.html88 login.html89 ...9091.. admonition:: Circular Imports9293 Every Python programmer hates them, and yet we just added some:94 circular imports (That's when two modules depend on each other. In this
5 unmodified lines
100101 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.104105106.. _working-with-modules:107108Working with Modules109--------------------110111For larger applications with more than a dozen views it makes sense to112split the views into module. First let's look at the typical struture 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 method,137but most of them are the same.138139Long story short, here 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 login():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 from yourapplication.views.admin import admin162 from yourapplication.views.frontend import frontend163164 app = Flask(__name__)165 app.register_module(admin)166 app.register_module(frontend)167168So what is different when working with modules? It mainly affects URL169generation. Remember the :func:`~flask.url_for` function? When not170working with modules it accepts the name of the function as first171argument. This first argument is called the "endpoint". When you are172working with modules you can use the name of the function like you did173without, when generating modules from a function or template in the same174module. If you want to generate the URL to another module, prefix it with175the name of the module and a dot.176177Confused? Let's clear that up with some examples. Imagine you have a178method in one module (say `admin`) and you want to redirect to a179different module (say `frontend`). This would look like this::180181 @admin.route('/to_frontend')182 def to_frontend():183 return redirect(url_for('frontend.index'))184185 @frontend.route('/')186 def index():187 return "I'm the frontend index"188189Now let's say we only want to redirect to a different module in the same190module. Then we can either use the full qualified endpoint name like we191did in the example above, or we just use the function name::192193 @frontend.route('/to_index')194 def to_index():195 return redirect(url_for('index'))196197 @frontend.route('/')198 def index():199 return "I'm the index"
modified:flask.py+16 added
141 unmodified lines
142143144def 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.147148 Here some examples:149150 ==================== ======================= =============================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 ==================== ======================= =============================159160 :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.top
230 unmodified lines
394395 @admin.route('/login')396 def login():397 pass398399 For a gentle introduction into modules, checkout the400 :ref:`working-with-modules` section.401 """402403 def __init__(self, import_name, name=None, url_prefix=None):404 if name is None:
591 unmodified lines