TurboGears/tg2

BaseController index method returns 404 if called with arguments

eteamin opened this issue · 1 comments

Consider this minimal controller

from tg import expose
from app.lib.base import BaseController
from app.model import Application


class ApplicationController(BaseController):

    @expose('path/to/template')
    def index(self, app_id):
        app = Application.one_or_none(app_id)
        return dict(app=app)

class RootController(BaseController):
    applications = ApplicationController()

if you serve this (localhost, 8080) and try http://localhost:8080/applications/argument1 you get a 404 because the router is looking for a method named argument1 in the ApplicationController. I would like to have http://localhost:8080/applications/1 to get the app with id = 1 but the mentioned behavior doesn't allow so. How to force the routing mechanism to consider the words after the applications/, argument for the index method, not a a method in the ApplicationController. I wish i had made my point.

amol- commented

index method is /something/index so you can do /something/index/a/b/c or something/index?a=1&b=1&c=1. For convenience the index method is also served as /something/ but no further.

What you are looking for is the _default method, which instead serves /someting/* at that point both /something/a/b/c/ and /something?a=1&b=1&c=1 would end up in _default (unless there is an index in that case /something?a=1&b=1&c=1 would end up in the index).

See http://turbogears.readthedocs.io/en/latest/turbogears/objectdispatch.html#the-default-method