Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All addresses to go to a single page (catch-all route to a single view) in Python Pyramid

Tags:

python

pyramid

I am trying to alter the Pyramid hello world example so that any request to the Pyramid server serves the same page. i.e. all routes point to the same view. This is what iv got so far:

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    return Response('Hello %(name)s!' % request.matchdict)

if __name__ == '__main__':
    config = Configurator()
    config.add_route('hello', '/*')
    config.add_view(hello_world, route_name='hello')
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    server.serve_forever()
   

All iv done is change the line (from the hello world example):

    config.add_route('hello', '/hello/{name}')

To:

    config.add_route('hello', '/*')

So I want the route to be a 'catch-all'. Iv tried various variations and cant get it to work. Does anyone have any ideas?

Thanks in advance

like image 617
joshlk Avatar asked Sep 03 '15 16:09

joshlk


1 Answers

The syntax for the catchall route (which is called "traversal subpath" in Pyramid) is *subpath instead of *. There's also *traverse which is used in hybrid routing which combines route dispatch and traversal. You can read about it here: Using *subpath in a Route Pattern

In your view function you'll then be able to access the subpath via request.subpath, which is a tuple of path segments caught by the catchall route. So, your application would look like this:

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    if request.subpath:
        name = request.subpath[0]
    else:
        name = 'Incognito'
    return Response('Hello %s!' % name)

if __name__ == '__main__':
    with Configurator() as config:
        config.add_route('hello', '/*subpath')
        config.add_view(hello_world, route_name='hello')
        app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8081, app)
    server.serve_forever()

Don't do it via custom 404 handler, it smells of PHP :)

like image 93
Sergey Avatar answered Sep 19 '22 22:09

Sergey