Routes

2.5.1 · active · verified Mon Apr 13

Routes is a Python library that re-implements the Rails routing system, enabling developers to map URLs to application actions and generate URLs. It focuses on creating clean, concise, and RESTful URLs with features like named routes, conditional matching, and sub-domain support. The library is currently in maintenance mode, meaning active development is limited, but bug fixes via pull requests are still accepted. The latest stable version is 2.5.1.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `Mapper`, connect routes using placeholders and default values, and then match incoming URLs to retrieve controller/action parameters or generate URLs from parameters.

from routes import Mapper

# Setup a mapper
map = Mapper()

# Connect routes
map.connect(None, '/error/{action}/{id}', controller='error')
map.connect('home', '/', controller='main', action='index')

# Match a URL
result = map.match('/error/myapp/4')
print(f"Match result for '/error/myapp/4': {result}")

result_home = map.match('/')
print(f"Match result for '/': {result_home}")

# Generate a URL
url = map.generate(controller='main', action='index')
print(f"Generated URL for main index: {url}")

error_url = map.generate(controller='error', action='display', id=123)
print(f"Generated URL for error: {error_url}")

view raw JSON →