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 7 (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.

Rendering Genshi Templates

TODO

Default Template Data

Diva makes a number of objects available for use in Genshi by default:

app
The Application object
abs_url()
Function for converting relative paths to absolute URLs
attrgetter
Function from the Python operator module
date
Python date class from the datetime module
datetime
Python datetime class from the datetime module
_() / gettext()
Function for translating a message
HTML()
Function to parse a string as HTML and return a markup stream
groupby
Function from the Python itertools module
itemgetter
Function from the Python operator module
ngettext()
Function for translating a message including plural forms
path_to()
Function for generating URLs based on the routing configuration
time
Python time class from the datetime module
timedelta
Python timedelta class from the datetime module
XML()
Function to parse a string as XML and return a markup stream

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

API Documentation

Error: Macro PythonDoc(trunk, diva.http) failed
Unsupported version control system "svn": No module named svn
Error: Macro PythonDoc(trunk, diva.templating) failed
Unsupported version control system "svn": No module named svn