Pergunta

Django is giving me a 404 error whenever I try to access "blog/" on my site, but I've defined the URLs I want and they should be matching that.

Main urls.py:

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

from django.contrib import admin
admin.autodiscover()
from blog import views

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mySiteProject.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

blog.urls.py:

from django.conf.urls import patterns,url
from blog import views

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

404 page:

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

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

    ^admin/

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

Site structure:

mySiteProject
    blog
        admin.py
        models.py
        tests.py
        views.py
        urls.py
        __init__.py
    mySiteProject
        wsgi.py
        settings.py
        urls.py
        __init__.py
    manage.py
    db.sqlite3

Installed apps:

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

    'blog'
)
Foi útil?

Solução

patterns requires a prefix as its first argument followed by zero or more arguments. So this:

urlpatterns = patterns(url(r'^$',views.index,name='index'))  # won't work

in blog.urls.py should look like this:

urlpatterns = patterns('', url(r'^$', views.index, name='index'))  # now has a prefix as first argument

In its present state, the patterns function in blog.urls.py will return an empty pattern_list, which means that url(r'^blog/', include('blog.urls')) will return no patterns.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top