Routing

Just define a tuple with three elements: url, handler and url name.

routes = (
    ('/', 'handlers.IndexHandler', 'index'),
)

Working with tokens

You can set a token in url definition, for example:

# file handlers.py
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler


class IndexHandler(RequestHandler):

    def get(self, name):
        self.render_template('index.html', name=name)
routes = (
    ('/{name}/', 'handlers.IndexHandler', 'index'),
    # convert to regex r'^/([^/]+)/$'
    # match '/allisson/'
    # match '/@allisson/'
)

For convinience, you can add a filter to token.

Example with int filter:

# file handlers.py
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler


class PostHandler(RequestHandler):

    def get(self, id):
        self.render_template('index.html', id=id)
routes = (
    ('/posts/{id:int}/', 'handlers.PostHandler', 'post_index'),
    # convert to regex r'^/posts/([\d]+)/$'
    # match '/posts/1/'
    # match '/posts/2/'
)

Example with string filter:

# file handlers.py
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler


class SearchHandler(RequestHandler):

    def get(self, query):
        self.render_template('index.html', query=query)
routes = (
    ('/search/{query:string}/', 'handlers.SearchHandler', 'search'),
    # convert to regex r'^/search/([\w]+)/$'
    # match '/search/mysearch/'
    # match '/search/my_search/'
)

Example with slug filter:

# file handlers.py
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler


class PostHandler(RequestHandler):

    def get(self, title):
        self.render_template('index.html', title=title)
routes = (
    ('/post/{title:slug}/', 'handlers.PostHandler', 'post'),
    # convert to regex r'^/post/([\w-]+)/$'
    # match '/post/my_post/'
    # match '/post/my-post/'
)

Example with path filter:

# file handlers.py
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler


class WikiHandler(RequestHandler):

    def get(self, title):
        self.render_template('index.html', wiki=wiki)
routes = (
    ('/wiki/{name:path}/', 'handlers.WikiHandler', 'wiki'),
    # convert to regex r'^/wiki/([^/].*?)/$'
    # match '/wiki/Allisson/Detail/'
    # match '/wiki/Allisson/Detail/Age/'
)

Example with re filter:

# file handlers.py
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler


class PostHandler(RequestHandler):

    def get(self, title):
        self.render_template('index.html', name=name)
routes = (
    ('/post/{name:re:([\w-@]+)}/', 'handlers.PostHandler', 'post'),
    # convert to regex r'^/post/([\w-@]+)/$'
    # match '/post/@allisson-azevedo/'
    # match '/post/@allisson_azevedo/'
)

Working with handler import

You can import the handler directly or inform the location

# file handlers.py
# -*- coding: utf-8 -*-
from gunstar.http import RequestHandler


class PostHandler(RequestHandler):

    def get(self, title):
        self.render_template('index.html')
from handlers import PostHandler

# inform location or import directly.
routes = (
    ('/post1/{title}/', 'handlers.PostHandler', 'post1'),
    ('/post2/{title}/', PostHandler, 'post2'),
)

Project Versions

Table Of Contents

Previous topic

Configuration

Next topic

Application

This Page