Ferris will automatically create a route name and url for every action in every handler in your application. You can also specify additional routes using the file app/routes.py.
Actions in your application are referenced using four parts:
These are called the route parts and are used to build both the route name and the url for a particular action.
For the route name, it follows the convention [prefix-]handler-action with handler being underscored.
For the url, it follows the convention [/prefix]/handler/action[/param_1, etc.] with handler being underscored.
The following table demonstrates various mappings:
Action | URL | name |
---|---|---|
Time.stop() | /time/stop | time-stop |
Daleks.exterminate(who) | /daleks/exterminate/<who> | daleks-exterminate |
Numbers.range(min, max) | /numbers/range/<min>/<max> | numbers-range |
Spaceships.xml_specs() | /xml/spaceships/specs | xml-spaceships-specs |
UserComments.json_stats | /json/user_comments/stats | json-user_comments-stats |
The methods named list, view, add, edit, and delete are always treated as actions and are implicitly routed (even when prefixed).
These methods have preset url mappings as follows, but can be prefixed:
Action | URL |
---|---|
list | /handler |
add | /handler/add |
view | /handler/<id> |
edit | /handler/<id>/edit |
delete | /handler/<id>/delete |
Other methods need to be explicitly routed using @route or @route_with.
Take the following methods for example:
def list(self):
return 'list'
@route
def test(self):
return 'test'
def run(self):
return 'run'
The methods list and test will be accessible via HTTP, but the method run is only accessible from code.
Marks a class method to enable it to be automatically routed and accessible via HTTP. This decorator should always be the outermost decorator.
To set a custom url for an action, use @route_with
Marks a class method to be routed and passes and additional arguments to the webapp2.Route constructor.
Parameters: | template – Sets the URL template for this action |
---|
For example:
@route_with(template='/ultimate/life/form')
def daleks(self):
return 'Daleks'
Prefixes must be explicitly listed in the prefix class property in a handler, for example:
class Posts(Handler):
prefixes = ['json']
@route
def json_stats(self):
pass
@route
def xml_stats(self):
pass
json_stats will have the url /json/posts/stats but xml_stats will be at /posts/xml_stats.
There is a standard way to generate URLs to actions across the application:
Generate in-application URIs (or URLs).
Parameters: |
|
---|
Examples:
uri('foxes-run') # -> /foxes/run
uri(prefix=False, handler='foxes', action='run') # -> /foxes/run
# when currently at /foxes/run
uri(action='hide') # -> /foxes/hide
Attempting to generate a URL to an action that doesn’t exist will result in an exception.
You can check for the existence of an action before attempting to generate a URL to it:
Check if a route exists.
You can see if you’re on a particular action. While this may seem like a superfluous feature, it’s very useful in templates:
Checks to see if we’re currently on the specified route.
Static files live in app/static and can be accessed via http://localhost:8080/static.
The folders css, js, and img are aliased and can be accessed via http://localhost:8080/css, http://localhost:8080/js, and http://localhost:8080/img respectively.
Plugin assets are available via http://localhost:8080/plugins/plugin_name/.