11120
11120

Reputation: 33

ImportError at /home/, No module named home.urls

Python 2.7 + Django 1.7

I'm getting:

Page not found (404)
Request Method:     GET
Request URL:    http://localhost:8000/home/

Using the URLconf defined in home.urls, Django tried these URL patterns, in this order:

^(?P<pk>\d+)/home/$ [name='home']
^(?P<pk>\d+)/services/$ [name='services']
^(?P<pk>\d+)/contact/$ [name='contact']

The current URL, home/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

home/urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin

from home import views

urlpatterns = patterns('', 
    url(r'^(?P<pk>\d+)/home/$', views.Home(), name='home'),
    url(r'^(?P<pk>\d+)/services/$', views.Services(), name='services'),
    url(r'^(?P<pk>\d+)/contact/$', views.Contact(), name='contact')
)

mysite/urls.py:

from django.conf import settings
from django.conf.urls.static import static

from django.conf.urls import patterns, include, url

from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^home/', include('mysite.home.urls', namespace="home")),
    url(r'^services/', include('home.urls', namespace="services")),
    url(r'^contact/', include('home.urls', namespace="contact"))
)

Why is this?

Project structure:

sitename
    db.sqlite3
    home
        __init.py
        admin.py
        models.py
        static
            home.html
            services.html
            contact.html
        urls.py
        views.py
    manage.py
    mysite
        __init.py
        settings.py
        urls.py
        wsgi.py

This doesn't include some pyc or pycache files. (I need to add more details to edit so I guess I'll just add some more text. Maybe this is what it wants. I don't know.)

Upvotes: 0

Views: 6010

Answers (2)

Ngenator
Ngenator

Reputation: 11269

home/urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin

from home import views

urlpatterns = patterns('', 
    url(r'^home/$', views.Home(), name='home'),
    url(r'^services/$', views.Services(), name='services'),
    url(r'^contact/$', views.Contact(), name='contact')
)

mysite/urls.py:

from django.conf import settings
from django.conf.urls.static import static

from django.conf.urls import patterns, include, url

from django.contrib import admin

admin.autodiscover()

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

I'm not sure if you actually want a pk for your home app, but if you do just change your root urlconf to this instead.

url(r'^(?P<pk>\d+)/', include('home.urls', namespace='mysite')),

You can catch up on the urlconf documentation here: https://docs.djangoproject.com/en/1.7/topics/http/urls/#including-other-urlconfs

Upvotes: 2

sahutchi
sahutchi

Reputation: 2233

Expanding on the comments from @Ngenator, your url patterns in your main urls.py file route everything to a url structure preceded by:

  • admin
  • home
  • services
  • contact

and reading into/ guessing what you are trying to do, you probably don't need:

(?P<pk>\d+)

You would use that when you wanted to grab that parameter pk in a views.py file like:

def my_view(request, pk):

Therefore, my guess is that you could have your url patterns defined in that one mysite/urls.py folder.

In my opinion it would not make sense to define services and contact in your home/urls.py file. You'd probably only need all of those other namespaces if you actually had apps for home, contacts, admin, etc.

Does that make sense? You don't need a urls.py defined for every single app. If you wanted to keep the urls.py structure for home/urls.py while maintaining a url structure like http://localhost.com:8000/home/, then in your home/urls.py you could just do something like:

url(r'^', home.views.Home(), name='home'),

under the assumption that your home/views.py file has a function like:

def Home(request):

Upvotes: 1

Related Questions