Yoo Matsuo
Yoo Matsuo

Reputation: 2363

Django - urlpatterns cannot find a module

I'm new to Django and from ASP.NET MVC. I just want to make a folder structure something below, though, if I navigate to http://localhost:8000/community_web/, that gives me an error saying "Could not import community_web.controllers.home. Error was: No module named controllers.home".

Folder structure what I want.

<project>
  urls.py
  <community_web>
    urls.py
    <controllers>
      home.py

I've added the following codes.
To project.urls.py

urlpatterns = patterns('',
    (r'^community_web/', include('community_web.urls')),
)

To project.community_web.urls.py

urlpatterns = patterns('',
    (r'^$', 'community_web.controllers.home.index'),
)

I thought a views.py would correspond to a controller in ASP.NET MVC term and so I don't want to put all request handlers into one file. If I moved the home.py to its parent folder, it works fine, but without a hierarchical folder structure, creating lots of files in one folder is not my preference. How can I achieve this? or maybe not a good practice in Django?

Thanks in advance,
Yoo

Upvotes: 0

Views: 343

Answers (3)

James Khoury
James Khoury

Reputation: 22329

Have you put 'community_web' into your installed apps section of your settings.py?

http://docs.djangoproject.com/en/dev/ref/settings/#installed-apps

INSTALLED_APPS = (                                                                 
    'django.contrib.auth',                                                         
    'django.contrib.contenttypes',                                                 
    'django.contrib.sessions',                                                     
    'django.contrib.sites',                                                        
    'django.contrib.admin',

    'community_web',
)

Upvotes: 0

Ken Cochrane
Ken Cochrane

Reputation: 77365

It is typically not good practice to do it that way. But do what you feel comfortable with. Under controllers do you have a init py file? If not it might not be able to find the home.py file.

Can you post the contents of home.py? There might be something in there that is causing it not load correctly.

Upvotes: 0

sdk900
sdk900

Reputation: 503

Try popping a __init__.py into the directory controllers. This will make the directory a module python can see.

Upvotes: 5

Related Questions