Page not found(404) - polls views Django tutorial

Asked

Viewed 228 times

0

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."

The problem is that when accessing http://127.0.0.1:8000/polls/34/, i get the bug:

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 gone over the code a zillion times, and I’m getting frustrated...

py Settings.

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 aren’t you 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 may be the python version being used?

1 answer

2

Is your last code even in polls.models? If so, that’s where the bug is. You need to create a file called 'urls.py' within the 'polls' app. This is how your project’s.py urls will include 'polls.urls' (check out the second piece of code you posted).

  • I put the name of the wrong folder, already arranged in the question, thanks... yes it was already in urls.py inside polls ...

  • I copied and pasted in VS code the code you provided in the question, I ran the server and everything worked out, including polls/34. The error is not in the code, but probably in some of the configuration steps, or in filenames. Make sure you registered the app in Settings.py/INSTALLED_APPS; also make sure you ran Manage.py migrate correctly. If nothing goes right, create a new project and copy and paste the code you posted here yourself. I did it and it worked. I hope it works.

  • When I go into /admin and /polls the pages work correctly, if the problem was in INSTALLED_APPS would it work? I mean, shouldn’t these 3 poll-derived pages just work with 'polls.apps.Pollsconfig' there? Thanks for the help.

  • Updating here, before used Pycharm to run, now using Vscode worked ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.