dashes in url

231 views
Skip to first unread message

David J.

unread,
Mar 31, 2011, 10:30:24 AM3/31/11
to web...@googlegroups.com
Seeing that using a "-" in function names is invalid in python;

I was wondering how I define in my controller "get-started" url;

If I define in my controller

def get-started():
return dict()

Its invalid; so I have to maybe use routes.py to rewrite it to just
getstarted()?

Is this the right approach;

Thanks

Ross Peoples

unread,
Mar 31, 2011, 10:31:58 AM3/31/11
to web...@googlegroups.com
web2py will automatically convert dashes to underscores for the function names, so:

getting-started

will become:

def getting_started():
    return dict()

David J.

unread,
Mar 31, 2011, 10:41:49 AM3/31/11
to web...@googlegroups.com
Thanks I will try that;

I get invalid request; but I will check again; perhaps I have a typo.

David J.

unread,
Mar 31, 2011, 10:44:43 AM3/31/11
to web...@googlegroups.com
I checked this;

I am using trunk;

I am defining

def get_started():
return dict()

def features():
return dict()


when I call /app/default/get-started

I get invalid request;

when I call

/app/default/features

It works perfectly;

Maybe a bug?

On 3/31/11 10:31 AM, Ross Peoples wrote:

David J.

unread,
Mar 31, 2011, 10:46:54 AM3/31/11
to web...@googlegroups.com
I missed that

calling app/default/get_started

works also;

On 3/31/11 10:31 AM, Ross Peoples wrote:

Ross Peoples

unread,
Mar 31, 2011, 10:46:48 AM3/31/11
to web...@googlegroups.com
You should be able to go to http://127.0.0.1:8000/[appname]/[controller]/getting-started and have the page show up. Replace [appname] with the name of your application, and [controller] with the name of the controller. So if your application is named 'myapp' and you are using the 'default' controller, this URL should work (also assuming that you are running the web2py on the same machine:


If you are wanting a shorter URL than that, then you may have to configure your routers.py file with your default application and default controller.

Jonathan Lundell

unread,
Mar 31, 2011, 10:53:08 AM3/31/11
to web...@googlegroups.com

The easiest thing to do is not use hyphens; make it get_started, perhaps.

Alternatively, the new router will by default let you use get-started in the URL, and translate it to get_started internally.


Jonathan Lundell

unread,
Mar 31, 2011, 10:54:20 AM3/31/11
to web...@googlegroups.com
On Mar 31, 2011, at 7:46 AM, David J. wrote:
>
> I missed that
>
> calling app/default/get_started
>
> works also;

The hyphen-underscore translation works only if you're running the new router.

David J

unread,
Mar 31, 2011, 11:26:45 AM3/31/11
to web...@googlegroups.com

Thanks I will rename router.py.example to routes.py in the web 2 py root and then I can also make a copy in my app to do specific routing?

VP

unread,
Mar 31, 2011, 12:24:59 PM3/31/11
to web2py-users

Unless I misunderstand the issue at hand here, seriously, I do not
think this is web2py's scope to do this automatic mapping hyphens or
dashes to underscores.

Hyphen/dash is a minus sign. You can't define a Python variable or
function that has a hyphen/dash. It's syntactically incorrect. I do
not think it's web2py's place to automatically fix this syntactical
error. (For one thing, it seems the side effect of this is that
underscores are automatically converted to hyphens, which is clearly
undesirable).

Perhaps, the best thing is letting people turn on this mapping
optionally.

David J.

unread,
Mar 31, 2011, 12:27:03 PM3/31/11
to web...@googlegroups.com
It is optional; read the router.py docs.

Jonathan mentioned that in his post.

Jonathan Lundell

unread,
Mar 31, 2011, 12:31:50 PM3/31/11
to web...@googlegroups.com
On Mar 31, 2011, at 9:27 AM, David J. wrote:
>
> It is optional; read the router.py docs.
>
> Jonathan mentioned that in his post.

VP is suggesting the opposite default, and I think perhaps he's right. It causes too much confusion.

Jonathan Lundell

unread,
Mar 31, 2011, 12:35:31 PM3/31/11
to web...@googlegroups.com
On Mar 31, 2011, at 9:24 AM, VP wrote:
>
> Unless I misunderstand the issue at hand here, seriously, I do not
> think this is web2py's scope to do this automatic mapping hyphens or
> dashes to underscores.
>
> Hyphen/dash is a minus sign. You can't define a Python variable or
> function that has a hyphen/dash. It's syntactically incorrect. I do
> not think it's web2py's place to automatically fix this syntactical
> error. (For one thing, it seems the side effect of this is that
> underscores are automatically converted to hyphens, which is clearly
> undesirable).
>
> Perhaps, the best thing is letting people turn on this mapping
> optionally.

Perhaps so.

FWIW, though, it's not a question of correcting a syntax error. The motivation is that underscores in URLs are unattractive, hard to type, and often hard to read (because of link underlining). The translation option is for having better-looking URLs.


Here's a suggestion for everyone who is using the new parametric router: if you care about hyphen translation one way or the other, turn it on or off explicitly for now, so a change to the default doesn't bite you. This is *only* a concern if you have (internally) underscores in your a/c/f names.

Anthony

unread,
Mar 31, 2011, 12:49:18 PM3/31/11
to web...@googlegroups.com
On Thursday, March 31, 2011 12:24:59 PM UTC-4, VP wrote:

Hyphen/dash is a minus sign.  You can't define a Python variable or
function that has a hyphen/dash.  It's syntactically incorrect.  I do
not think it's web2py's place to automatically fix this syntactical
error.
 
Note, web2py is not correcting a syntax error. web2py expects your controller functions to obey proper Python syntax and not include any hyphens. But it assumes that when you include underscores in an app, controller, or function name that you want those underscores converted to hyphens in outgoing URLs (that's a reasonable assumption, as it is a preferred standard for URLs). It therefore also assumes that when an incoming URL includes hyphens, those must map to underscores in the matching app, controller, and function names, because the names would not be legal Python names with hyphens.
 
I guess the problem is that you *can* include hyphens in app names (as you have) without (necessarily) violating any Python syntax, so if your app name includes hyphens, the router mistakenly thinks the hyphens in the app name in incoming URLs should be mapped to underscores.
 
Anthony

pbreit

unread,
Mar 31, 2011, 1:09:16 PM3/31/11
to web...@googlegroups.com
I guess I can see leaving the default to re-map hyphens to underscores but perhaps only on controllers?

pbreit

unread,
Mar 31, 2011, 1:10:51 PM3/31/11
to web...@googlegroups.com
Sorry, I think I meant "functions".

Jonathan Lundell

unread,
Mar 31, 2011, 1:25:30 PM3/31/11
to web...@googlegroups.com
On Mar 31, 2011, at 10:09 AM, pbreit wrote:
> I guess I can see leaving the default to re-map hyphens to underscores but perhaps only on controllers?

Well, functions for sure. I'm not sure about applications and controllers.

Anthony

unread,
Mar 31, 2011, 1:40:41 PM3/31/11
to web...@googlegroups.com
I would probably lean toward at least functions *and* controllers. I think it's common to have underscores (but probably not hyphens) in controller names (for example, any plugin controller that follows the "plugin_" naming convention).

Anthony

unread,
Mar 31, 2011, 1:44:33 PM3/31/11
to web...@googlegroups.com
What's the recommendation on app names? Should we aim for app names that are valid Python names? If, for example, you want to import a module from another app, does the app name have to be a valid Python name?

On Thursday, March 31, 2011 1:25:30 PM UTC-4, Jonathan Lundell wrote:

Jonathan Lundell

unread,
Mar 31, 2011, 2:10:16 PM3/31/11
to web...@googlegroups.com
On Mar 31, 2011, at 10:44 AM, Anthony wrote:
What's the recommendation on app names? Should we aim for app names that are valid Python names? If, for example, you want to import a module from another app, does the app name have to be a valid Python name?

That's a good point; I don't know. Presumably it does; you definitely get a syntax error if you have a hyphen in an import statement.

The whole applications/ tree is set up as a two-level package, with __init__.py files (but not in controllers/).

I think it's prudent to require a/c/f to be valid Python identifiers.

VP

unread,
Mar 31, 2011, 3:52:47 PM3/31/11
to web2py-users

> I guess I can see leaving the default to re-map hyphens to underscores but
> perhaps only on functions?

YES.

App names are file names, and people generally don't want to have
hyphens in their file names, which is the opposite of what they would
want for URLs.

As for app names, I think they should be left alone because the
typical convention for file naming is underscored, which is the
opposite of what is desired for URLs. In other words, if people name
their files with underscores, it wouldn't be reasonable to assume they
want hyphens.

VP

unread,
Mar 31, 2011, 4:02:24 PM3/31/11
to web2py-users
I should have written "directory names" instead of "file names" (even
though they are related).

The conflict is apparent when web2py and other things (Apache, etc.)
work together. For example, Apache routes the static files whereas
web2py routes the dynamic things.

When you say URL('static', 'style.css'), web2py will assume that app-
names should be converted to hyphens. But its routing will interfere
with the conventional assumption of Apache, which is underscores are
meant to be underscores.

Jonathan Lundell

unread,
Mar 31, 2011, 4:04:31 PM3/31/11
to web...@googlegroups.com

What if they want hyphens? While app names are indeed file names, they're also used for other purposes within web2py. As somebody pointed out, they might well appear in an import statement, in which case they'd need to be valid Python identifiers--no hyphens.

I think we ought to stay safe and require that a/c/f all be valid Python identifiers, and give users the option of presenting underscores as hyphens in URLs. I agree, though, that translation should probably not be the default (as it is now).

Jonathan Lundell

unread,
Mar 31, 2011, 4:10:30 PM3/31/11
to web...@googlegroups.com
On Mar 31, 2011, at 1:02 PM, VP wrote:
>
> I should have written "directory names" instead of "file names" (even
> though they are related).
>
> The conflict is apparent when web2py and other things (Apache, etc.)
> work together. For example, Apache routes the static files whereas
> web2py routes the dynamic things.
>
> When you say URL('static', 'style.css'), web2py will assume that app-
> names should be converted to hyphens. But its routing will interfere
> with the conventional assumption of Apache, which is underscores are
> meant to be underscores.

In that case, you'd either want to turn off hyphen translation or use the Apache rewrite function.

BTW, I see that web2py's local_import function uses the application name as part of a module name. So app names must be legal Python module identifiers.

Anthony

unread,
Mar 31, 2011, 4:25:50 PM3/31/11
to web...@googlegroups.com
On Thursday, March 31, 2011 3:52:47 PM UTC-4, VP wrote:

> I guess I can see leaving the default to re-map hyphens to underscores but
> perhaps only on functions?

YES.

App names are file names, and people generally don't want to have
hyphens in their file names, which is the opposite of what they would
want for URLs.
 
Yes, that's exactly the point of the web2py router default behavior. People generally do not want hyphens in their app/folder, controller, or function names (at least in part because hyphens aren't allowed in Python identifiers), but they also do not want underscores in their URLs. Without any rewriting, you'd either have to have hyphens in your app, controller, and function names (which is undesirable/illegal), or you'd have to have underscores in your URLs (also undesirable). The current default router behavior allows you to use underscores in the internal names yet still have hyphens in the URLs. So, if you have app = 'my_app', controller = 'my_controller.py', and action = 'my_action()', then you get a URL like /my-app/my-controller/my-function (likewise, an incoming URL like that gets properly mapped to the internal names with underscores). If you stick to the convention of only using underscores in the internal names (including the app name), then everything works great with the current default.
 
As for app names, I think they should be left alone because the
typical convention for file naming is underscored, which is the
opposite of what is desired for URLs.  In other words, if people name
their files with underscores, it wouldn't be reasonable to assume they
want hyphens.
 
The router assumes if you name your app with underscores that you *do* want hyphens when the app name is displayed in a URL, and that does seem reasonable.
 
Best,
Anthony
 

Anthony

unread,
Mar 31, 2011, 4:37:03 PM3/31/11
to web...@googlegroups.com
On Thursday, March 31, 2011 4:04:31 PM UTC-4, Jonathan Lundell wrote:

What if they want hyphens? While app names are indeed file names, they're also used for other purposes within web2py. As somebody pointed out, they might well appear in an import statement, in which case they'd need to be valid Python identifiers--no hyphens.

I think we ought to stay safe and require that a/c/f all be valid Python identifiers, and give users the option of presenting underscores as hyphens in URLs. I agree, though, that translation should probably not be the default (as it is now).

I think the current default makes sense, given that in most cases we would want the translation turned on (particularly if we are encouraging/requiring that a/c/f all be valid Python identifiers).

VP

unread,
Mar 31, 2011, 6:33:02 PM3/31/11
to web2py-users

> The router assumes if you name your app with underscores that you *do* want
> hyphens when the app name is displayed in a URL, and that does seem
> reasonable.
>
> Best,
> Anthony

The problem is when I name my app with underscore but do not want
hyphens in the URL. As mentioned above, an example is when I use
Apache to serve static files. In such a scenario, I do want
URL('static', 'file.txt') to be 'my_app/static/file.txt' NOT 'my-app/
static/file.txt'. This is because Apache has no idea what 'my-app/
static/file.txt' is.

As Johnathan said, I can use mod-rewrite in Apache to rewrite, but it
doesn't make sense to require people do this by default.

I think the pretty URL display setting should be optional instead of
default. Generally speaking, I think behaviors that are different
from conventionally expected should be default behaviors. Pretty URLs
are desirable, but underscored filenames are conventional.

(desirable means nobody gets hurts if it's not done as such;
conventional means certainly will go bad if not done as such).

Anthony

unread,
Mar 31, 2011, 6:59:28 PM3/31/11
to web...@googlegroups.com
OK, I think you make a good point. Leaving the current default behavior (i.e., automatic translation between underscores and hyphens) can actually cause things to break, whereas turning off the translation by default will merely result in ugly URLs (but nothing will break). That's probably a good argument for changing the default behavior.
 
Still, it would be nice if there were an easy way to handle your situation with Apache serving static files (e.g., by specifying an translation exclusion for URLs involving the static "controller").
 
Anthony

pbreit

unread,
Mar 31, 2011, 11:12:26 PM3/31/11
to web...@googlegroups.com
I've come around a little bit on the default. The one thing where I could see defaulting to re-mapping is with the function since that's not going to work anyhow.
Reply all
Reply to author
Forward
0 new messages