Question

Well, I use Python Bottle framework and I want to create a root path for every kind of links like the following:

/py-admin
/py-admin/
/py-admin/<pagename>

I tried with this one, but I have a 404 error

@bottle.get("/py-admin/<pagename>")
def py_admin(pagename=None):
    if pagename == "download":
       do sth  
       return .....  
    elif pagename == "update":
       do sth else
       return .......
    return .....

So, if the link has a second path, then an if will applied. Otherwise, if the /py-admin or py-admin/ is the link, then the final return will be called.

Any hint how can I fix it? I prefer to not create different path roots if it is possible.

Was it helpful?

Solution

did you simply try the following?

@bottle.get("/py-admin")
@bottle.get("/py-admin/")
@bottle.get("/py-admin/<pagename>")
def py_admin(pagename=None):
    # your code…

that's actually one of the examples of the tutorial:

which is the way you go, if really you "prefer to not create different path roots"

but if your code really looks like your short example, then instead of:

@bottle.get("/py-admin/<pagename>")
def py_admin(pagename=None):
    if pagename == "download":
       do sth  
       return .....  
    elif pagename == "update":
       do sth else
       return .......
    return .....

I really think you should consider doing:

@bottle.get("/py-admin")
@bottle.get("/py-admin/")
def py_admin():
    # … do something
    return ……

@bottle.get("/py-admin/download")
@bottle.get("/py-admin/download/")
def py_admin_download():
    # … do something
    return ……


@bottle.get("/py-admin/update")
@bottle.get("/py-admin/update/")
def py_admin_update():
    # … do something
    return ……

but in the end it's really up to you, and it really depends on your code.

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top