close Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 4 (modified by cmlenz, 16 years ago) (diff)

--

Request Handlers

A request handler is a neutral name for what some other frameworks call “controllers” or “views”. It is simply a Python function that accepts the request and response objects as parameters, and returns a response.

from diva.templating import output, render

@output('index.html')
def index(request, response):
    return render(message='Hello, world!')

This defines a function called index that will respond to requests by rendering the template index.html.

URL Parts as Parameters

Any dynamic parts of the URLs defined in the UrlRouting are passed to the request handler as additional parameters. For example:

from datetime import datetime
from diva import app
from diva.templating import output, render

@output('index.html')
def monthly(request, response, year, month):
    return render(month=datetime(year, month, 1))

app.routing.add('/archives/{year:\d+}/{month:\d+}/', monthly)

Note that in this particular case, the values for year and month are automatically converted to numbers, due to the use of the \d+ pattern in the route path.

Builtin Request Handlers

Diva comes with a couple of generic request handlers that can be used as-is with some parameterization.

diva.routing:delegate
Delegate request processing to a different WSGI application.
diva.routing:redirect
Send an HTTP redirect to a different URL
diva.static:directory
Serve static files from a specific directory
diva.static:file
Serve a specific static file
diva.templating:view
Render a given template