0
votes

This has been asked before, but the problems were usually with placement of the mysite/urls.py or missing text somewhere. I've gone over those in detail, but doesn't apply here. I'm following the django tutorial EXACTLY, which means it hasn't referenced including the polls app in the settings.py file. I can pull up the right view if I manually type in the "polls" at the end of the url, as in "http://127.0.0.1:8000/polls" but I shouldn't have to do this for it to work. I'm also assuming the tutorial isn't wrong in some way. Link to the tutorial is: https://docs.djangoproject.com/en/1.9/intro/tutorial01/

The error I get:

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

My views.py file:

from django.shortcuts import render
from django.http import HttpResponse

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

My polls/urls.py file:

from django.conf.urls import url
from . import views

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

My mysite/mysite/urls.py file:

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

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

]

This is the tree for the mysite/mysite directory just to show that I am in the right folder (there is no separate urls.py file in the main mysite directory):

.
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-35.pyc
│   ├── settings.cpython-35.pyc
│   ├── urls.cpython-35.pyc
│   └── wsgi.cpython-35.pyc
├── settings.py
├── urls.py
└── wsgi.py

Again, best I can tell this is to the letter following the django tutorial guidance. I'd like to fix it, but more importantly understand why it isn't working.

1
"I can pull up the right view if I manually type in the "polls" at the end of the url, ... but I shouldn't have to do this for it to work.". Why do you think you shouldn't need to do that? That looks like what I'd expect based on your two urls.py files. - Tom Dalton
docs.djangoproject.com/en/1.9/intro/tutorial01/… from that tutorial you linked, the end of that section says "Go to localhost:8000/polls in your browser, and you should see ...". So the tutorial does tell you to hit the /polls url. - Tom Dalton
The tutorial even tells you that you should see the view load at /polls/. This is defined in your main urls.py. If you want to change it...you should be using: url(r'', include('polls.urls')), - rnevius
I thought that when the mysite/urls.py file found the polls/urls.py file it would then call the views.py index function and the localhost:8000/polls page with "Hello, world" would be the default. Much more to learn, I guess. Thank you both for your help. I did take try the 'url(r' '.....)' and it worked. - rejo

1 Answers

0
votes

Have you checked the INSTALLED_APPS variable in the settings file to include the 'polls' app?