TurboGears2: TypeError: redirect() got an unexpected keyword argument 'pagename'

351 Views Asked by At

This error occurs when following the TG2 wiki20.

I could not see an easy answer on here that was related to TG2, and this issue could be quite confusing, as it occurs during the official tutorial.

The problem is when using the tg.redirect method, as described in the tutorial:

@expose('wiki20.templates.page')
def _default(self, pagename="FrontPage"):
    from sqlalchemy.exc import InvalidRequestError

    try:
        page = DBSession.query(Page).filter_by(pagename=pagename).one()
    except InvalidRequestError:
        raise redirect("notfound", pagename=pagename)

    content = publish_parts(page.data, writer_name="html")["html_body"]
    root = url('/')
    content = wikiwords.sub(r'<a href="%s\1">\1</a>' % root, content)
    return dict(content=content, wikipage=page)

Trying to use the method above, copied exactly from the tutorial returns the error. The issue is clearly with the keyword arguments accepted by the redirect method.

1

There are 1 best solutions below

0
nihilok On

I was able to solve this problem by checking the docs for the redirect method itself:

tg.controllers.util.redirect(base_url='/', params=None, redirect_with=<class 'tg.exceptions.HTTPFound'>, scheme=None, **kwargs)

You can that it accepts a params parameter (and NOT **kwargs -> perhaps in an earlier version, **kwargs was accepted). Unfortunately, the params argument is not type-hinted, but I guessed it should be a dict, and was correct.

The working method, as far as I can tell, should look like this:

    @expose('wiki20.templates.page')
    def _default(self, pagename="FrontPage"):
        from sqlalchemy.exc import InvalidRequestError

        try:
            page = DBSession.query(Page).filter_by(pagename=pagename).one()
        except InvalidRequestError:
            raise redirect("notfound", params={'pagename': pagename})

        content = publish_parts(page.data, writer_name="html")["html_body"]
        root = url('/')
        content = wikiwords.sub(r'<a href="%s\1">\1</a>' % root, content)
        return dict(content=content, wikipage=page)

EDIT:

There is a "latest" version of the tutorial, which DOES include the change above, exactly as it is here (but it wasn't the default version for me when I first accessed the tutorial for some reason).
I will leave this here in case it helps any other googlers who didn't check their version numbers!