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 3 (modified by cmlenz, 14 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

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

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.