Platforms: Unix, Windows
This is an API reference, NOT documentation. If you are new to bottle, have a look at the Tutorial.
The module defines several functions, constants, and an exception.
Bottle maintains a stack of Bottle instances (see app() and AppStack) and uses the top of the stack as a default application for some of the module-level functions and decorators.
Return a generator for routes that match the signature (name, args) of the func parameter. This may yield more than one route if the function takes optional keyword arguments. The output is best described by example:
a() -> ‘/a’ b(x, y) -> ‘/b/:x/:y’ c(x, y=5) -> ‘/c/:x’ and ‘/c/:x/:y’ d(x=5, y=6) -> ‘/d’ and ‘/d/:x’ and ‘/d/:x/:y’
Shift path fragments from PATH_INFO to SCRIPT_NAME and vice versa.
Returns: | The modified paths. |
---|---|
Parameters: |
|
WSGI application
Decorator: bind a function to a GET request path.
If the path parameter is None, the signature of the decorated function is used to generate the paths. See yieldroutes() for details.
The method parameter (default: GET) specifies the HTTP request method to listen to. You can specify a list of methods too.
The Request class wraps a WSGI environment and provides helpful methods to parse and access form data, cookies, file uploads and other metadata. Most of the attributes are read-only.
The Response class on the other hand stores header and cookie data that is to be sent to the client.
Note
You usually don’t instantiate Request or Response yourself, but use the module-level instances bottle.request and bottle.response only. These hold the context for the current request cycle and are updated on every request. Their attributes are thread-local, so it is safe to use the global instance in multi-threaded environments too.
Represents a single HTTP request using thread-local attributes. The Request object wraps a WSGI environment and can be used as such.
Cookie information parsed into a dictionary.
Secure cookies are NOT decoded automatically. See Request.get_cookie() for details.
The QUERY_STRING parsed into a MultiDict.
Keys and values are strings. Multiple values per key are possible. See MultiDict for details.
Property: The HTTP POST body parsed into a MultiDict.
This supports urlencoded and multipart POST requests. Multipart is commonly used for file uploads and may result in some of the values being cgi.FieldStorage objects instead of strings.
Multiple values per key are possible. See MultiDict for details.
HTTP authorisation data as a (user, passwd) tuple. (experimental)
This implementation currently only supports basic auth and returns None on errors.
Bind a new WSGI enviroment.
This is done automatically for the global bottle.request instance on every request.
The HTTP request body as a seekable buffer object.
This property returns a copy of the wsgi.input stream and should be used instead of environ[‘wsgi.input’].
HeaderDict filled with request headers.
HeaderDict keys are case insensitive str.title()d
Shift path fragments from PATH_INFO to SCRIPT_NAME and vice versa.
Parameter: | shift – The number of path fragments to shift. May be negative to change the shift direction. (default: 1) |
---|
Full URL as requested by the client (computed).
This value is constructed out of different environment variables and includes scheme, host, port, scriptname, path and query string.
Represents a single HTTP response using thread-local attributes.
Return the charset specified in the content-type header.
This defaults to UTF-8.
Add a new cookie with various options.
If the cookie value is not a string, a secure cookie is created.
All template engines supported by bottle implement the BaseTemplate API. This way it is possible to switch and mix template engines without changing the application code at all.
Base class and minimal API for template adapters
Decorator: renders a template for a handler. The handler can control its behavior like that:
- return a dict of template vars to fill out the template
- return something other than a dict and the view decorator will not process the template, but return the handler result as is. This includes returning a HTTPResponse(dict) to get, for instance, JSON with autojson or other castfilters
You can write your own adapter for your favourite template engine or use one of the predefined adapters. Currently there are four fully supported template engines:
Class | URL | Decorator | Render function |
---|---|---|---|
SimpleTemplate | SimpleTemplate Engine | view() | template() |
MakoTemplate | http://www.makotemplates.org | mako_view() | mako_template() |
CheetahTemplate | http://www.cheetahtemplate.org/ | cheetah_view() | cheetah_template() |
Jinja2Template | http://jinja.pocoo.org/ | jinja2_view() | jinja2_template() |
To use MakoTemplate as your default template engine, just import its specialised decorator and render function:
from bottle import mako_view as view, mako_template as template