tags:

views:

577

answers:

2

I'm following the Django Tutorials, I'm at the end of part 3, at Decoupling the URLconfs, at http://docs.djangoproject.com/en/1.1/intro/tutorial03/#intro-tutorial03 and I'm getting a "No module named urls" error message.

When I change:

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('mysite.polls.views',
    (r'^polls/$', 'index'),
    (r'^polls/(?P<poll_id>\d+)/$', 'detail'),
    (r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
    (r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
    (r'^admin/', include(admin.site.urls)),
)

to:

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^polls/', include('mysite.polls.urls')),
    (r'^admin/', include(admin.site.urls)),
)

I changed include('mysite.polls.urls')), to include(mysite.polls.urls)),, but it still didn't work.

How to solve this problem?

UPDATE 1: The full error message is at http://dpaste.com/166201/

UPDATE 2: at mysite/polls/urls.py is

from django.conf.urls.defaults import *

urlpatterns = patterns('mysite.polls.views',
    (r'^$', 'index'),
    (r'^(?P<poll_id>\d+)/$', 'detail'),
    (r'^(?P<poll_id>\d+)/results/$', 'results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

UPDATE 3: the really full error message splitted in screenshots is at

http://i.imagehost.org/0191/python_1.png
http://i.imagehost.org/0426/python_2.png
http://g.imagehost.org/0191/python_3.png
http://g.imagehost.org/0413/python_4.png
http://i.imagehost.org/0370/python_5.png
http://i.imagehost.org/0317/python_6.png

UPDATE 4: the whole project is at

http://www.mediafire.com/?t1jvomjgjz1

+2  A: 

Is there an __init__.py inside mysite/polls/ directory?

chromano
There is, but it's empty. Is that right?
Delirium tremens
Empty is fine, that just tells Python the directory is a package. Make sure there's an `__init__.py` in the parent `mysite` directory too
Joe Holloway
There is too and by the way it's empty too.
Delirium tremens
I'm going to assume there is one. If Delirium is following the Django tutorial, he probably used the `djangoadmin.py`/`manage.py` commands to create the site and app. They place the `__init__.py` in there by default. As far as I can tell, Delirium has had a working site, using the `polls` module, up until he tried decoupling the URLConf from the site and placing it in the app. This seems to indicate that there's an issue as to the placement of the `urls.py` file and how it's called.
David Antaramian
I've a question and a suggestion (that fix the problem).Question: why can't you just use include('mysite.pools.urls') ?Suggestion: You can try to import mysite.pools.urls before, i.e "from mysite.pools import urls as pool_urls" and use "include(pool_urls)"
chromano
chromano, what is the difference if import mysite.polls.urls before? by the way, it's really polls. http://dictionary.reference.com/browse/polls
Delirium tremens
I would second trying to import it. To add on to that, you can use `python manage.py shell` to open a django terminal shell and then try `from mysite.polls import urls`. If you can import it, than the problem is definitely something in the syntax of `mysite/urls.py`.
David Antaramian
@David - according to his answer to my comment on the main question, he doesn't get an error when importing the urls from the shell.
Lance McNearney
@Lance Didn't see that. Thanks :)
David Antaramian
@delirium-tremens sorry about the mistake. In python you just can't import a module name and refer to others modules imported by it, i.e, you can't do `import site` and use `site.polls.urls`, you should import `site.polls.urls` module, eg: `import site.polls.urls` and then use `site.polls.urls`, got it? btw, is it working now?
chromano
it's working now!
Delirium tremens
+1  A: 

I can't re-produce the import error on my machine using your project files (Windows 7, Django 1.1.1, Python 2.6.4). Everything imported fine but the urls were not specified properly (like the tutorial shows). Fixing the code:

/mysite/urls.py:

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^polls/', include('mysite.polls.urls')),
    (r'^admin/', include(admin.site.urls)),
)

/mysite/polls/urls.py:

from django.conf.urls.defaults import *

urlpatterns = patterns('mysite.polls.views',
    (r'^$', 'index'),
    (r'^(?P<poll_id>\d+)/$', 'detail'),
    (r'^(?P<poll_id>\d+)/results/$', 'results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

Visit http://127.0.0.1:8000/polls/ - I received a TemplateDoesNotExist exception because the template file is missing.

I'm afraid my answer might be to reboot and try it again. ;)

Lance McNearney
If he's using WSGI or something.... Django likes to super-cache the heck out of everything... I ran into so many headaches with that when I first started developing with Django..... dev server saves a lot of pain.
Mark
@Mark - He's going through the tutorial so I can't imagine setting up WSGI to serve it. Plus his SERVER_SOFTWARE (screenshot 3) is set to "WSGIServer/0.1 Python/2.6.4", which matches what my machine serves when using the dev server.
Lance McNearney
I was just like you said, "the urls were not specified properly (like the tutorial shows)", they really weren't.
Delirium tremens
Oh I figured.... but I was just sayin' ;) I tried developing on a live server. Not good.
Mark