Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 Error in "Writing your first Django App" tutorial

Tags:

python

django

I am new to programming and have been working through the "Writing your first Django App" Tutorial for version 1.8. I'm stuck with an error.

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/polls
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, polls, didn't match any of these.

I've seen others with a similar error but did not see a resolution. I even went so far as to simply copy and paste what is in the tutorial for the files in this section (the top part of section 3) but still haven't seen a resolution.

Mysite\polls\urls.py

from django.conf.urls import url

from . import views

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

Mysite\urls.py

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

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
]

Mysite\polls\views.py

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

Mysite\settings.py

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

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'
...

I have no idea where the disconnect is but my guess based on the error is that it is with the urls.py file in the main mysite directory... it doesn't seem to read the urlpatterns for polls? How could this be resolvd? (I should note I tried this with and without the final "/" after polls for the url in the browser).

like image 529
Benjamin Avatar asked May 27 '15 20:05

Benjamin


1 Answers

The root urls should be at mysite\mysite\urls.py, not mysite\urls.py as you have it.

like image 177
Alasdair Avatar answered Sep 19 '22 15:09

Alasdair