Django Tutorial Part 3, Test after Update of Polls/urls.py Fails

I’m following the Django tutorial as a first time user, but I’m getting stuck at [part 3](https://docs.djangoproject.com/en/3.2/intro/tutorial03/).

I believe the goal of the tutorial is to input a value into the address bar, like poll/34/, which will run a method found in the view.py. The following steps (included my code) are:

1. add a few more views to polls/views.py .

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


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


def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

2. I Wire these new views into the polls.urls module

from django.urls import path
from . import views

urlpatterns = [
    # ex: /polls/
    path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

3. Take a look in your browser, at “/polls/34/”. It’ll run the detail() method and display whatever ID you provide in the URL.

The 3rd step does not work, when I enter http://localhost:8000/polls/34/ (while running the server), I get a 404 error. I also tried http://127.0.0.1:8000/34/.

Can somebody help me out?

Please post your project’s urls.py file. (There should be an entry in it that includes your poll’s urls.)

u mean this one? How would I do that?

"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path


urlpatterns = [
    path('admin/', admin.site.urls),
]

Go back to part 1 and review the instructions regarding your mysite urls.py file.

1 Like

The docstring of the code you shared also contains the instructions on how to include another app’s URLconf.

I re-read the first part of the tutorial and I understand now that I had to add path(‘polls/’, include(‘polls.urls’)), to the protect urls.py file. Thx @KenWhitesell for the quick reply.