Confusion with ROOT_URLCONF

1,050 views
Skip to first unread message

Roy Smith

unread,
Apr 3, 2011, 9:11:55 AM4/3/11
to Django users
I don't understand how ROOT_URLCONF is declared in settings.py. If I
put all my apps (and my settings.py file) in a directory "foo", I'm
supposed to do:

ROOT_URLCONF = "foo.urls"

This seems counter-intuitive to me. When I run my app (by running
"python manage.py runserver"), I'm already in the "foo" directory.
From there, shouldn't you just need to do "import urls", not "import
foo.urls"? Is the server doing a "cd .." at some point before it
starts running my code?

andy

unread,
Apr 3, 2011, 2:55:06 PM4/3/11
to Django users
Well I'm guess you don't have to. Both ROOT_URLCONF = "foo.urls" and
ROOT_URLCONF = "urls" seem to work fine.

Roy Smith

unread,
Apr 3, 2011, 4:22:09 PM4/3/11
to Django users
On Apr 3, 2:55 pm, andy <flowar...@gmail.com> wrote:
> Well I'm guess you don't have to. Both ROOT_URLCONF = "foo.urls" and
> ROOT_URLCONF = "urls" seem to work fine.

Interesting, I just tried it that way, and sure enough it does work.
I had simply been following the examples in the tutorial, which showed
the foo.urls form. Perhaps it once had to be that way in an older
version and the docs just never got updated?

Now I'm curious how it works both ways.

Shawn Milochik

unread,
Apr 3, 2011, 4:58:30 PM4/3/11
to django...@googlegroups.com
In short, it has to be on your PYTHONPATH or in the local directory.
If you're importing from the local directory it always works (assuming
the subdirectory contains a file named __init__.py). The other path,
'foo.urls' works because 'foo' is in your PYTHONPATH.

Do whatever makes sense to you and won't blow up in your deployment
environment.

In Python, it's generally considered better to import using the full
path to prevent name collisions. However, if you're making a re-usable
Django app that will be imported in another project where you won't
know the full path, you may have to do "local" imports in your
views.py.

Shawn

Roy Smith

unread,
Apr 3, 2011, 7:15:56 PM4/3/11
to Django users
On Apr 3, 4:58 pm, Shawn Milochik <sh...@milochik.com> wrote:
> In short, it has to be on your PYTHONPATH or in the local directory.

OK, then I'm still not getting how this works. The full path to my
setting and urls files are:

/Users/roy/s7/soco/soco-site/settings.py
/Users/roy/s7/soco/soco-site/urls.py

If I have in that settings file:

ROOT_URLCONF = 'soco-site.urls'

and start up my server from the soco-site directory, it finds the urls
file. How? If I print out sys.path from inside settings.py, it
includes /Users/roy/S7/soco/soco-site, but not /Users/roy/S7/soco.
Even stranger, "soco-site" is not a valid module name. If I do
something like "import soco-site.urls", I (as expected) get a syntax
error. So what's the process that gets from the string 'soco-
site.urls' to a valid module import? Is django handing the string
'soco-site.urls' directly to imp.loadmodule(), or something like that,
or is it parsing the string in django application code and doing
something magic with it?

andy

unread,
Apr 3, 2011, 9:42:09 PM4/3/11
to Django users
I'm not really a pro with the who path stuff but this is how I see it.

ROOT_URLCONF = 'projectname.urls'

In my project I can import projectname and projectname.urls.
generally all django project have a __init__.py file which makes it a
module, so in your case soco-site should be a valid module with a
normal django setup.

You said that '/Users/roy/S7/soco/soco-site' was printed which sounds
just about correct since that would allow you to import the soco-site
module. So you should have been able to do "import soco-site" and
"import soco-site.urls". If you will type "import url" python will
look if it belong to any of the directories on the python path, and it
would therefore find in under '/Users/roy/S7/soco/soco-site'. If
multiple directories had a url.py then there would be some conflict,
maybe If you have multiple django projects for example, which is why
I'm guessing django specifies it using the project name. Or it could
just be conversion since this is the recommended way as Shawn said.

Since
Reply all
Reply to author
Forward
0 new messages