Default parameter is not working?

12 views
Skip to first unread message

Raj

unread,
Jul 7, 2015, 2:08:12 PM7/7/15
to turbo...@googlegroups.com
Hi,
I have a TG app with following controllers:
  • RootController
    • MediaController
      • News Controller
In the RootController i have added subcontroller as media=MediaController()
In MediaController i have added news=NewsController()

NewsController is having an index() method with a parameter named 'url'

The NewsController()->index() page is displaying when the following URLs are invoked
But the following URL shows a 404 error
I am expecting this should display the same result as of the URL http://localhost:8080/media/news/index/<value-of-url-param>
What is happening while removing index from URL?

What is the solution?

Regards,
Raj

Michael Pedersen

unread,
Jul 9, 2015, 7:10:01 AM7/9/15
to turbo...@googlegroups.com
This comes down to a misunderstanding of what happens with the / character in URLs and how to use _lookup. I don't know that I have good examples for how to use _lookup, so I can't help there, but the other part I can explain:

TG separates the URL based on the / character. It goes to the top most controller (RootController), and says "Do you know how to handle /media". The RootController has a MediaController named "media", so it goes to that controller and asks about the next item: news. Since that controller has a NewsController named "news", it goes to the NewsController and asks it how to handle the next part.

You have a method named index, so whenever the next part is named "index", it passes everything to that method. That covers "/index/<value-of-param>" and "index?url=<value-of-param>". If there is nothing after the final / TG assumes you want to get the index method called, so uses that method. That covers "news/" and even "news?url=<value-of-param>" since there's a hidden / between "news" and "?url=" (that's how TG interprets it).

The final one, though, doesn't cover any of these cases. "news/<value-of-param>" doesn't actually match anything. NewsController is asked "do you have any attribute named <value-of-param>" and it doesn't, so it can't process it. It returns a 404. What you would need to do is add a method named _lookup() that would then tell TG that the index method knows how to handle <value-of-param>.

As I said, I don't have examples to share for this right now, so I can't do much to give code. I'm sorry. But at least this provides an explanation for what to do next.

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.
To post to this group, send email to turbo...@googlegroups.com.
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.

Moritz Schlarb

unread,
Jul 9, 2015, 8:03:27 AM7/9/15
to turbogears
The _default and _lookup methods are explained here:
http://turbogears.readthedocs.org/en/latest/turbogears/objectdispatch.html
The difference is basically that _default returns a "page" like normal @expose'd
methods, whereas _lookup returns a new controller object where the dispatching
process continues.

> Michael Pedersen <m.ped...@icelus.org> hat am 9. Juli 2015 um 13:09
> geschrieben:
> > I have a TG app with following *controllers*:
> >
> > - RootController
> > - MediaController
> > - News Controller
> >
> > In the RootController i have added subcontroller as *media*
> > =MediaController()
> > In MediaController i have added *news*=NewsController()
> >
> > NewsController is having an index() method with a parameter named 'url'
> >
> > The NewsController()->index() page is displaying when the following URLs
> > are invoked
> >
> > - http://localhost:8080/media/news/
> > - http://localhost:8080/media/news/index*/<value-of-url-param>*
> > - http://localhost:8080/media/news/index*?url=<value-of-url-param>*
> > - http://localhost:8080/media/news*?url=<value-of-url-param>*
> >
> > But the following URL shows a* 404 error*
> >
> > - http://localhost:8080/media/news*/<value-of-url-param>*
> >
> > I am expecting *this should display the same result* as of the URL
> > http://localhost:8080/media/news*/index/<value-of-url-param>*
> > What is happening while removing *index *from URL?

Raj

unread,
Jul 9, 2015, 8:20:47 AM7/9/15
to turbo...@googlegroups.com
Dear Michale Pedersen and Moritz Schlarb,
Thanks for your replies. It helps me to understand TG better.

Regards,
Raj

(^!^)
Raj

You received this message because you are subscribed to a topic in the Google Groups "TurboGears" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/turbogears/ek1z85Nm5rA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to turbogears+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages