Page not found ( 404) - polls views django tutorial

I'm doing the Django documentation tutorial, in Part 3 ( https://docs.djangoproject.com/en/3.1/intro/tutorial03 / ) where it is said after adding some codes the following:

"Take a look in your browser, at "/polls/34/". It'll run the detail() method and display whatever ID you provide in the URL. Try "/polls / 34 / results/ " and "/polls / 34 / vote/ " too - these will display the placeholder results and voting pages."

A problem is that when accessing http://127.0.0.1:8000/polls/34 / , I get the error:

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/polls/34 / Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

Admin/ polls /[name= 'index'] The current path, polls/ 34/, didn't match any of these.

I've reviewed the code zillions of times, and to me frustrate...

Settings.py

ROOT_URLCONF = 'mysite.urls'

Mysite / urls

from django.contrib import admin
from django.urls import path, include

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

Polls / views

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.


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)

Polls/urls.py

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'),
]

Why isn't ta working???

If any code is missing please let me know .

Att: I found that the problem is in PyCharm, since in vs code everything worked right, the problem can be the version of python being used?

Author: João Paulo Blume, 2020-09-26

1 answers

Your Last code is even in polls.models? If so, that's where the error lies. You need to create a file called 'urls.py' inside the 'polls' app. It is in this way that the urls.py of your project will include the 'polls.urls' (check out the second piece of code you posted).

 2
Author: fsimoes, 2020-09-26 04:20:41