From a36e8152279ada9478f16202aeccebf6bed59132 Mon Sep 17 00:00:00 2001 From: Suby Raman Date: Thu, 23 Feb 2017 11:42:59 -0500 Subject: [PATCH 1/2] Add documentation about `request.app` --- docs/sanic/request_data.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/sanic/request_data.md b/docs/sanic/request_data.md index c1eea5c1..8a4f281c 100644 --- a/docs/sanic/request_data.md +++ b/docs/sanic/request_data.md @@ -69,6 +69,23 @@ The following variables are accessible as properties on `Request` objects: - `ip` (str) - IP address of the requester. +- `app` - a reference to the Sanic application object that is handling this request. This is useful when inside blueprints or other handlers in modules that do not have access to the global `app` object. + +```python +from sanic.response import json +from sanic import Blueprint + +bp = Blueprint('my_blueprint') + +@bp.route('/') +async def bp_root(request): + if request.app.config['DEBUG']: + return json({'status': 'debug'}) + else: + return json({'status': 'production'}) + +``` + ## Accessing values using `get` and `getlist` The request properties which return a dictionary actually return a subclass of From 4232f5342e037e60633314e376abfe386b70c4ed Mon Sep 17 00:00:00 2001 From: Suby Raman Date: Thu, 23 Feb 2017 11:47:39 -0500 Subject: [PATCH 2/2] correct indendation --- docs/sanic/request_data.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/sanic/request_data.md b/docs/sanic/request_data.md index 8a4f281c..f411ed8d 100644 --- a/docs/sanic/request_data.md +++ b/docs/sanic/request_data.md @@ -71,20 +71,20 @@ The following variables are accessible as properties on `Request` objects: - `app` - a reference to the Sanic application object that is handling this request. This is useful when inside blueprints or other handlers in modules that do not have access to the global `app` object. -```python -from sanic.response import json -from sanic import Blueprint + ```python + from sanic.response import json + from sanic import Blueprint -bp = Blueprint('my_blueprint') + bp = Blueprint('my_blueprint') -@bp.route('/') -async def bp_root(request): - if request.app.config['DEBUG']: - return json({'status': 'debug'}) - else: - return json({'status': 'production'}) + @bp.route('/') + async def bp_root(request): + if request.app.config['DEBUG']: + return json({'status': 'debug'}) + else: + return json({'status': 'production'}) -``` + ``` ## Accessing values using `get` and `getlist`