forked from orbit-oss/flask
Fixed merging confict
This commit is contained in:
commit
eeb0e94951
9 changed files with 147 additions and 13 deletions
|
|
@ -183,6 +183,13 @@ To access the current session you can use the :class:`session` object:
|
|||
# so mark it as modified yourself
|
||||
session.modified = True
|
||||
|
||||
.. attribute:: permanent
|
||||
|
||||
If set to `True` the session life for
|
||||
:attr:`~flask.Flask.permanent_session_lifetime` seconds. The
|
||||
default is 31 days. If set to `False` (which is the default) the
|
||||
session will be deleted when the user closes the browser.
|
||||
|
||||
|
||||
Application Globals
|
||||
-------------------
|
||||
|
|
|
|||
|
|
@ -91,3 +91,52 @@ Here the code::
|
|||
|
||||
Notice that this assumes an instanciated `cache` object is available, see
|
||||
:ref:`caching-pattern` for more information.
|
||||
|
||||
|
||||
Templating Decorator
|
||||
--------------------
|
||||
|
||||
A common pattern invented by the TurboGears guys a while back is a
|
||||
templating decorator. The idea of that decorator is that you return a
|
||||
dictionary with the values passed to the template from the view function
|
||||
and the template is automatically rendered. With that, the following
|
||||
three examples do exactly the same::
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html', value=42)
|
||||
|
||||
@app.route('/')
|
||||
@templated('index.html')
|
||||
def index():
|
||||
return dict(value=42)
|
||||
|
||||
@app.route('/')
|
||||
@templated()
|
||||
def index():
|
||||
return dict(value=42)
|
||||
|
||||
As you can see, if no template name is provided it will use the endpoint
|
||||
of the URL map + ``'.html'``. Otherwise the provided template name is
|
||||
used. When the decorated function returns, the dictionary returned is
|
||||
passed to the template rendering function. If `None` is returned, an
|
||||
empty dictionary is assumed.
|
||||
|
||||
Here the code for that decorator::
|
||||
|
||||
from functools import wraps
|
||||
from flask import request
|
||||
|
||||
def templated(template=None):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
template_name = template
|
||||
if template_name is None:
|
||||
template_name = request.endpoint + '.html'
|
||||
ctx = f(*args, **kwargs)
|
||||
if ctx is None:
|
||||
ctx = {}
|
||||
return render_template(template_name, **ctx)
|
||||
return decorated_function
|
||||
return decorator
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ In the view function, the usage of this form looks like this::
|
|||
form.password.data)
|
||||
db_session.add(user)
|
||||
flash('Thanks for registering')
|
||||
redirect(url_for('login'))
|
||||
return redirect(url_for('login'))
|
||||
return render_template('register.html', form=form)
|
||||
|
||||
Notice that we are implying that the view is using SQLAlchemy here
|
||||
|
|
|
|||
|
|
@ -160,6 +160,8 @@ The following converters exist:
|
|||
`path` like the default but also accepts slashes
|
||||
=========== ===========================================
|
||||
|
||||
.. _url-building:
|
||||
|
||||
URL Building
|
||||
````````````
|
||||
|
||||
|
|
@ -167,7 +169,8 @@ If it can match URLs, can it also generate them? Of course you can. To
|
|||
build a URL to a specific function you can use the :func:`~flask.url_for`
|
||||
function. It accepts the name of the function as first argument and a
|
||||
number of keyword arguments, each corresponding to the variable part of
|
||||
the URL rule. Here some examples:
|
||||
the URL rule. Unknown variable parts are appended to the URL as query
|
||||
parameter. Here some examples:
|
||||
|
||||
>>> from flask import Flask, url_for
|
||||
>>> app = Flask(__name__)
|
||||
|
|
@ -184,9 +187,11 @@ the URL rule. Here some examples:
|
|||
... print url_for('index')
|
||||
... print url_for('login')
|
||||
... print url_for('profile', username='John Doe')
|
||||
... print url_for('login', next='/')
|
||||
...
|
||||
/
|
||||
/login
|
||||
/login?next=/
|
||||
/user/John%20Doe
|
||||
|
||||
(This also uses the :meth:`~flask.Flask.test_request_context` method
|
||||
|
|
@ -452,7 +457,7 @@ transmitted in a `POST` or `PUT` request) you can use the
|
|||
:attr:`~flask.request.form` attribute. Here a full example of the two
|
||||
attributes mentioned above::
|
||||
|
||||
@app.route('/login', method=['POST', 'GET'])
|
||||
@app.route('/login', methods=['POST', 'GET'])
|
||||
def login():
|
||||
error = None
|
||||
if request.method == 'POST':
|
||||
|
|
|
|||
|
|
@ -195,3 +195,22 @@ suite.
|
|||
|
||||
.. _MiniTwit Example:
|
||||
http://github.com/mitsuhiko/flask/tree/master/examples/minitwit/
|
||||
|
||||
|
||||
Other Testing Tricks
|
||||
--------------------
|
||||
|
||||
Besides using the test client we used above there is also the
|
||||
:meth:`~flask.Flask.test_request_context` method that in combination with
|
||||
the `with` statement can be used to activate a request context
|
||||
temporarily. With that you can access the :class:`~flask.request`,
|
||||
:class:`~flask.g` and :class:`~flask.session` objects like in view
|
||||
functions. Here a full example that showcases this::
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
with app.test_request_context('/?name=Peter'):
|
||||
assert flask.request.path == '/'
|
||||
assert flask.request.args['name'] == 'Peter'
|
||||
|
||||
All the other objects that are context bound can be used the same.
|
||||
|
|
|
|||
|
|
@ -2,8 +2,7 @@ Bonus: Testing the Application
|
|||
===============================
|
||||
|
||||
Now that you have finished the application and everything works as
|
||||
expected, it's probably not the best idea to add automated tests to
|
||||
simplify modifications in the future. The application above is used as a
|
||||
basic example of how to perform unittesting in the :ref:`testing` section
|
||||
of the documentation. Go there to see how easy it is to test Flask
|
||||
applications.
|
||||
expected, it's probably not a good idea to add automated tests to simplify
|
||||
modifications in the future. The application above is used as a basic
|
||||
example of how to perform unittesting in the :ref:`testing` section of the
|
||||
documentation. Go there to see how easy it is to test Flask applications.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue