Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Jul 21, 2016
1 parent 3c3cf0e commit 0c32338
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
11 changes: 5 additions & 6 deletions demos/polls/aiohttpdemo_polls/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@


def setup_routes(app, project_root):
add_route = app.router.add_route
add_route('GET', '/', index)
add_route('GET', '/poll/{question_id}', poll, name='poll')
add_route('GET', '/poll/{question_id}/results',
results, name='results')
add_route('POST', '/poll/{question_id}/vote', vote, name='vote')
app.router.add_route('GET', '/', index)
app.router.add_route('GET', '/poll/{question_id}', poll, name='poll')
app.router.add_route('GET', '/poll/{question_id}/results',
results, name='results')
app.router.add_route('POST', '/poll/{question_id}/vote', vote, name='vote')
app.router.add_static('/static/',
path=str(project_root / 'static'),
name='static')
46 changes: 29 additions & 17 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ and second table is choice table:
| question_id |
+---------------+

TBD: aiopg.sa.create_engine and pushing it into app's storage

TBD: graceful cleanup


.. _aiohttp-tutorial-views:

Expand All @@ -156,16 +160,17 @@ next Python code inside file (``polls/aiohttpdemo_polls/views.py``)::
from aiohttp import web


class SiteHandler:
async def index(self, request):
return web.Response(text='Hello Aiohttp!')
async def index(self, request):
return web.Response(text='Hello Aiohttp!')

This is the simplest view possible in Aiohttp. Now we should add ``index`` view
to ``polls/aiohttpdemo_polls/routes.py``::

def setup_routes(app, handler, project_root):
add_route = app.router.add_route
add_route('GET', '/', handler.index)
from .views import index


def setup_routes(app, project_root):
app.router.add_route('GET', '/', index)

Now if we open browser we can see::

Expand All @@ -181,17 +186,18 @@ Templates
Let's add more useful views::

@aiohttp_jinja2.template('detail.html')
async def poll(self, request):
question_id = request.match_info['question_id']
try:
question, choices = await db.get_question(self.postgres,
question_id)
except db.RecordNotFound as e:
raise web.HTTPNotFound(text=str(e))
return {
'question': question,
'choices': choices
}
async def poll(request):
async with request['db'].acquire() as conn:
question_id = request.match_info['question_id']
try:
question, choices = await db.get_question(conn,
question_id)
except db.RecordNotFound as e:
raise web.HTTPNotFound(text=str(e))
return {
'question': question,
'choices': choices
}

Templates are very convinient way forweb page writing. We return a
dict with page content, ``aiohttp_jinja2.template`` decorator
Expand Down Expand Up @@ -235,3 +241,9 @@ Fortunatelly it can be done easy by single call::


where ``project_root`` is the path to root folder.


Middlewares
-----------

TBD

0 comments on commit 0c32338

Please sign in to comment.