packages.rst
This file
Path
docs/patterns/packages.rst
199 lines · 6.1 KiB
Last changed by
Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoptione429b69
History of this fileTake it
1.. _larger-applications:23Larger Applications4===================56For larger applications it's a good idea to use a package instead of a7module. That is quite simple. Imagine a small application looks like8this::910 /yourapplication11 /yourapplication.py12 /static13 /style.css14 /templates15 layout.html16 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)2728You should then end up with something like that::2930 /yourapplication31 /yourapplication32 /__init__.py33 /static34 /style.css35 /templates36 layout.html37 index.html38 login.html39 ...4041But how do you run your application now? The naive ``python42yourapplication/__init__.py`` will not work. Let's just say that Python43does not want modules in packages to be the startup file. But that is not44a big problem, just add a new file called `runserver.py` next to the inner45`yourapplication` folder with the following contents::4647 from yourapplication import app48 app.run(debug=True)4950What did we gain from this? Now we can restructure the application a bit51into multiple modules. The only thing you have to remember is the52following quick checklist:53541. the `Flask` application object creation has to be in the55 `__init__.py` file. That way each module can import it safely and the56 `__name__` variable will resolve to the correct package.572. all the view functions (the ones with a :meth:`~flask.Flask.route`58 decorator on top) have to be imported when in the `__init__.py` file.59 Not the object itself, but the module it is in. Do the importing at60 the *bottom* of the file.6162Here's an example `__init__.py`::6364 from flask import Flask65 app = Flask(__name__)6667 import yourapplication.views6869And this is what `views.py` would look like::7071 from yourapplication import app7273 @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 this95 case `views.py` depends on `__init__.py`). Be advised that this is a96 bad idea in general but here it is actually fine. The reason for this is97 that we are not actually using the views in `__init__.py` and just98 ensuring the module is imported and we are doing that at the bottom of99 the file.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 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 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 function 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"