smilebomb
smilebomb

Reputation: 5483

django urlconf 404 on webroot

I'm getting a 404 on just the webroot. I don't know what's wrong with my urlconf.

Directory structure:

myproject        // from django-admin startproject
  myproject      // ''                          ''
    __init__.py
    settings.py
    urls.py
    views.py
  myapp          // from manage.py startapp
    migrations
    __init__.py
    admin.py
    models.py
    tests.py
    urls.py
    views.py

In myproject > urls.py:

from django.conf.urls import include, url, patterns
from myapp import urls as app_urls


urlpatterns = patterns('',
    url(r'^/$', include(app_urls)),
)

And in myapp > urls.py:

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

urlpatterns = patterns('',
    url(r'^/$', 'index'),
)

I run the django development server and I get a 404 on the index page, even though I have the empty regex defined.

Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^/$
The current URL, , didn't match any of these.

The documentation in urls.conf from a a fresh install of django:

Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))

Changing the regex in BOTH urls.py files to r'^' appears to work, but with a new 'Could not import 'index'. error.

Upvotes: 1

Views: 166

Answers (2)

César
César

Reputation: 10119

Don't use $, when using include. Change it to:

# myproject/urls.py

from django.conf.urls import include, url, patterns
from myapp import urls as app_urls


urlpatterns = patterns('',
    url(r'^', include(app_urls)),
)

# myapp/urls.py

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

urlpatterns = patterns('',
    url(r'^$', 'index'),
)

Upvotes: 1

John Gordon
John Gordon

Reputation: 33335

The root URL is ^$, not ^/$.

Also, when you include a urlconf from another urlconf, it uses both patterns together; it doesn't forget the original pattern. So your app is really looking for the url ^/$^/$, which I'm not sure can ever be matched.

Upvotes: 0

Related Questions