Skip to main content

About tgext.routes

https://travis-ci.org/TurboGears/tgext.routes.png https://coveralls.io/repos/TurboGears/tgext.routes/badge.png https://img.shields.io/:license-mit-blue.svg?style=flat-square

tgext.routes provides a simple way to integrate routes based dispatch into TurboGears2 applications.

Installing

tgext.routes can be installed both from pypi or from bitbucket:

pip install tgext.routes

should just work for most of the users

Routing Single Actions

Routes matching is done through the @route decorator, each exposed method can be bound to one or multiple routes.

The only requirement is that you inherit from the RoutedController instance.

All routes registered through the @route decorator are registered starting from the controller mount point.

The following example registers the entry_by_date method for urls like /date/2012-01 and /date/2012-01-01:

from tgext.routes import RoutedController, route

class DateController(RoutedController):
    @expose()
    @route('{year}-{month}', day=33)
    @route('{year}-{month}-{day}')
    def entry_by_date(self, year, month, day):
        return '%s ++ %s ++ %s' % (year, month, day)

class RootController(BaseController):
    date = DateController()

    @expose()
    def index(self):
        return 'Hello!'

Keep in mind that as @expose wraps it, the method is still accessible through ObjectDispatch routing, @route can just register additional routes.

If there is a route pointing to it, also actions that do not provide an @expose decoration are resolved, keep in mind that in that case you will have to render template manually.

In case you want to disable ObjectDispatch you can set disable_objectdispatch=True inside the controller. Keep in mind that it will disable ObjectDispatch for the whole controller and so you won’t be able to dispatch actions that do not provide a route from that controller on.

Routing Whole application

RoutedController can also be mounted as the application RootController. In that case instead of using the @route decorator you can even provide a routes.Mapper object as controller mapper attribute and register all the routes of your application:

class RootController(RoutedController):
    mapper = Mapper()
    mapper.connect('/', controller='home', action='index')
    mapper.connect('/json', controller='home', action='jsonexposed')
    mapper.connect('/unex', controller='home', action='unexposed')

In this case the controller argument is required and controller will be looked up inside the path specified by tg.config['paths']['controllers']. In the previous example a HomeController class will be looked for into the home.py module to serve the /, /json and /unex paths through its index, jsonexposed and unexposed methods.

In case both a mapper attribute and @route decorator are used inside the same RoutedController, the @route decorator is applied after the mapper routes.

For more documentation about routes refer to Routes Documentation

Continuing with ObjectDispatch

When using tgext.routes as the primary routing method it is possible to tell it to continue with ObjectDispatch (or any specific dispatch method provided by the target controller) using _dispatch as the action of the route.

By default as routes are matched exactly providing a _dispatch action will provide no remainder, so it will only dispatch to index method of the target controller. To continue dispatching with a remaining part of the url you can use an _dispatch label:

mapper.connect('/forsub{_dispatch:.*?}', controller='home',  action='_dispatch')

This will dispatch to HomeController continuing with ObjectDispatch for anything that cames after /forsub part of the url.

Metadata

Release files for tgext.routes 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tgext.routes 0.2.2
File Size Uploaded Provenance Actions
tgext_routes-0.2.2.tar.gz 6.1 kB Without provenance Details for tgext_routes-0.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tgext.routes 0.2.2
File Interpreter ABI Platform Actions
tgext_routes-0.2.2-py3-none-any.whl Python 3 none any Details for tgext_routes-0.2.2-py3-none-any.whl

Total release size: 12.5 kB

Release files / tgext_routes-0.2.2.tar.gz

Download URL tgext_routes-0.2.2.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3a1246a5c15cf814b4d2c64b8367b3eaf0260e2eb23da865b071280a2ee7db69
BLAKE2b-256 checksum
How to use checksums
98d251f40f071f6fcdfb4e7df5c72118248379a915939e1bc77a240dec1c4d6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Release files / tgext_routes-0.2.2-py3-none-any.whl

Download URL tgext_routes-0.2.2-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e987f2b081a30f9ab2b69ca39d683b4a4d29fbc59e6fb1122c283cb672cab8a9
BLAKE2b-256 checksum
How to use checksums
ffec51b5debdb453a8800a1ff5657110682d5f19fb7d121c726323c7fdae7f76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Security for tgext.routes 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page