routes, removing appname and controller from all links

180 views
Skip to first unread message

jjg0

unread,
Mar 1, 2013, 9:42:57 AM3/1/13
to web...@googlegroups.com
I've been having a lot of trouble trying to clean up my URLs.  If my app name is myapp, and I go out an buy the domain myapp.com, when I deploy the site all the urls are going to read www.myapp.com/myapp/default/function.  How do I get rid of the app name and controller?

I've read the section in the book but did not find it very helpful.  If I set

default_application = 'myapp'    # ordinarily set in base routes.py
default_controller = 'default'  # ordinarily set in app-specific routes.py
default_function = 'index'      # ordinarily set in app-specific routes.py

This works for the index function, so if I go to my local server 127.0.0.1:8000 I will see the page from myapp/default/index.  But if I add another page, for example myapp/default/contactus, I want to be able to just enter 127.0.0.1:8000/contactus instead of 127.0.0.1:8000/myapp/default/contactus.

I saw in another question someone posted an example using the parameter based system:


routers = dict(

    # base router
    BASE=dict(
        default_application='myapp',
    ),

    myapp=dict(
        default_controller='default',
        default_function='index',
        functions=['index', 'contactus'],
    ),
)

where I guess you have to type in every function in the default controller?  This still doesn't solve my problem.  Using these settings does the same thing where going to 127.0.0.1:8000 will take you to 127.0.0.1:8000/myapp/default/index, but trying any other page like 127.0.0.1:8000/contactus does not work.  I still need to use 127.0.0.1:8000/myapp/default/<function> for any additional pages I add.

The pattern based system seems a bit more confusing.  Do people typically use this over the parameter one?  Is the parameter one more of a crutch for new users?  I tried adding to routes_in:

('/contactus', '/myapp/default/contactus'),

in hopes that using 127.0.0.1:8000/contactus would take me to 127.0.0.1:/8000/myapp/default/contactus, but this seems to have no effect.  Are there better tutorials on how to use route_in/out? Will I have to add an entry to routes for every page I add, or are there shortcuts?

Thanks

*One side note, I saw routes.py needs to be reloaded every time it is changed, so I have been restarting the server when testing these examples.





Jonathan Lundell

unread,
Mar 1, 2013, 10:51:29 AM3/1/13
to web...@googlegroups.com
On 1 Mar 2013, at 6:42 AM, jjg0 <miahg...@gmail.com> wrote:
I saw in another question someone posted an example using the parameter based system:


routers = dict(

    # base router
    BASE=dict(
        default_application='myapp',
    ),

    myapp=dict(
        default_controller='default',
        default_function='index',
        functions=['index', 'contactus'],
    ),
) 

where I guess you have to type in every function in the default controller? 

Only if you want to omit function names at the same time you have args following the function. Otherwise, no need. To accomplish what you're after, all you need (because default/index is already the deafult) is:

routers = dict(

    # base router
    BASE=dict(
        default_application='myapp',
    ),

This still doesn't solve my problem.  Using these settings does the same thing where going to 127.0.0.1:8000 will take you to 127.0.0.1:8000/myapp/default/index, but trying any other page like 127.0.0.1:8000/contactus does not work. 

It should. Be sure to restart your server/web2py after editing routes.py, though.

jjg0

unread,
Mar 1, 2013, 11:26:41 AM3/1/13
to web...@googlegroups.com
As I mentioned, I am restarting the server with every change to routes.py.  I tried using:


routers = dict(

    # base router
    BASE=dict(
        default_application='myapp',
    ),
)

But this still does not solve my problem.  I need every page to hide the app name and controller, not just the default index page.  Right now I am just trying to get this working in an empty project, so my default controller looks like:

def index():
    return dict()

def contactus():
    return dict()

and the two corresponding views are just using {{extend 'layout.html'}} and then left empty at the moment.

Using this and the above routes.py example, I can go to 127.0.0.1.8000 on my local server and I will see the index page without having to add /myapp/default/index to the url.  So it looks like the routes.py is doing something right, however if I try 127.0.0.1:8000/contactus I get an invalid request.  Using 127.0.0.1:8000/myapp/default/contactus works fine, but I don't want the app and controller in the url.

Hope my explanation of the problem is clear.

Thanks

Rufus

unread,
Mar 1, 2013, 5:49:46 PM3/1/13
to web...@googlegroups.com
# As simple as this?   (I'm new too)

routes_in = (
    ('/$anything', '/myapp/default/$anything'),
)

routes_out = (
    ('/myapp/default/$anything', '/$anything'),
)


jjg0

unread,
Mar 1, 2013, 7:14:57 PM3/1/13
to web...@googlegroups.com
Oh I wish it were that simple, but no I still see invalid requests when trying that:( 

Can anyone help me with this?  I still have not resolved this issue.

Jonathan Lundell

unread,
Mar 1, 2013, 7:21:40 PM3/1/13
to web...@googlegroups.com
On 1 Mar 2013, at 4:14 PM, jjg0 <miahg...@gmail.com> wrote:
Oh I wish it were that simple, but no I still see invalid requests when trying that:(  

Can anyone help me with this?  I still have not resolved this issue.

When you use the parametric router, what exactly is the complete error message (please specify the URL as well)?

Rufus Smith

unread,
Mar 1, 2013, 7:25:38 PM3/1/13
to web...@googlegroups.com
I anticipated that, because I tried it.

I added:

routes_onerror = [
   (r'*/*', r'error')
]

to call an error function. in this case, /myapp/default/error
--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/IjE2kSJ9j68/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Message has been deleted

jjg0

unread,
Mar 1, 2013, 8:21:39 PM3/1/13
to web...@googlegroups.com, richard...@lavabit.com
Ok, I think I see what I did wrong.  On further investigation, it turns out that resetting the server is not good enough to refresh the routes.py file, you need to actually reload it in the admin interface(I am running the web2py source on a windows pc, maybe this is different on linux?). The first answer from Jonathan actually does work, I just wasn't reloading routes.py correctly.  I thought stopping and starting the server would work, or even restarting the pc.  Turns out I was wrong:( 

Thanks everyone for taking the time to help me out with this!


On Friday, March 1, 2013 7:34:36 PM UTC-5, rh wrote:
On Fri, 1 Mar 2013 16:14:57 -0800 (PST)
jjg0 <miahg...@gmail.com> wrote:

> Oh I wish it were that simple, but no I still see invalid requests
> when trying that:(  
>
> Can anyone help me with this?  I still have not resolved this issue.
>

Why not just specify them explicitly?

routes_in = (
    ('/myapp', '/myapp/default/index'),
    ('/contactus', '/contactus/default/index'),
)

Also, I've used this to simplify my routes_out:

routes_out = [(x, y) for (y, x) in routes_in]

I enabled logging too, renamed logging.example.conf -> logging.conf
and restarted.

>
>
>
> On Friday, March 1, 2013 5:49:46 PM UTC-5, Rufus wrote:
> >
> > # As simple as this?   (I'm new too)
> >
> > routes_in = (
> >     ('/$anything', '/myapp/default/$anything'),
> > )
> >
> > routes_out = (
> >     ('/myapp/default/$anything', '/$anything'),
> > )
> >
> >
> >
>
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "web2py-users" group. To unsubscribe from this group and stop
> receiving emails from it, send an email to web2py
> +unsub...@googlegroups.com. For
> more options, visit https://groups.google.com/groups/opt_out.
>
>


--


Reply all
Reply to author
Forward
0 new messages