getting 404 page not found on part 3 of the tutorial

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

getting 404 page not found on part 3 of the tutorial

George P. Olson
Hi guys,
New here to django. Going through the tutorial, on part 3, I set up the mysite/polls/urls.py file  like this according to the instructions:

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

Then I set up the /mysite/polls/views.py file like this according to the instructions:

-----
from django.http import HttpResponse
from .models import Question

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    output = ', '.join([q.question_text for q in latest_question_list])
    return HttpResponse(output)

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)
-----

So when I go to this url: http://127.0.0.1:8000/polls/

I get this:

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

  1. admin/
  2. polls [name='index']
  3. polls <int:question_id>/ [name='detail']
  4. polls <int:question_id>/results/ [name='results']
  5. polls <int:question_id>/vote/ [name='vote']

The current path, polls/, didn't match any of these.


I cannot see what the error is that I am making. Any ideas? Thanks in advance.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/112f7336-14e3-9e47-850b-3601ebecec52%40gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: getting 404 page not found on part 3 of the tutorial

Clive Bruton

On 11 Nov 2020, at 23:37, George P. Olson wrote:

> So when I go to this url: http://127.0.0.1:8000/polls/

Although the urlpatterns below shows "# ex: /polls/", the defined  
path has no path elements, so you just need:

http://127.0.0.1:8000/

I think.

> urlpatterns = [
>     # ex: /polls/
>     path('', views.index, name='index'),...


-- Clive

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/83D6D705-ACA5-49C1-B421-2E4729414B71%40indx.co.uk.
Reply | Threaded
Open this post in threaded view
|

Re: getting 404 page not found on part 3 of the tutorial

George P. Olson
I don't quite follow your note there of just going to:
without the app name 'polls' following.

I believe you have to put the app name following that. It was working before with http://127.0.0.1:8000/polls/, but then I did something and tried to backtrack and the whole system got messed up. I tried to reset everything back to the original but it is still not working.

On Wednesday, November 11, 2020 at 7:47:58 PM UTC-6 Clive Bruton wrote:

On 11 Nov 2020, at 23:37, George P. Olson wrote:

> So when I go to this url: http://127.0.0.1:8000/polls/

Although the urlpatterns below shows "# ex: /polls/", the defined
path has no path elements, so you just need:

http://127.0.0.1:8000/

I think.

> urlpatterns = [
> # ex: /polls/
> path('', views.index, name='index'),...


-- Clive

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1feb0c4a-8919-4aff-abb1-e0110392c396n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: getting 404 page not found on part 3 of the tutorial

Clive Bruton
Sorry, I just checked my version of the tutorial, you load the  
manage.py through the directory "mysite", and "polls" is in a sub-
directory. The way you showed the "/polls/urls.py" implied that was  
the only file, where in fact the root app/settings are at "/mysite/
settings.py" and urls at "/mysite/urls.py".

Because "/mysite/urls.py" has an include for "polls/urls.py" it  
implies the path "/polls".

It's a bit tricky to diagnose, because you are now saying it was  
working, but then you changed stuff. My directory setup looks like this:

/mysite/manage.py
...
/mysite/mysite/settings.py
...
/mysite/mysite/urls.py
...
/mysite/polls/settings.py
...
/mysite/polls/urls.py

I checked my polls.py, which looks like this:

*************

from django.urls import path

from . import views

app_name = 'polls'

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

urlpatterns = [
     path('', views.IndexView.as_view(), name='index'),
     path('<int:pk>/', views.DetailView.as_view(), name='detail'),
     path('<int:pk>/results/', views.ResultsView.as_view(),  
name='results'),
     path('<int:question_id>/vote/', views.vote, name='vote'),
]

***************

I think I read recently that generic views like "IndexView" need a  
".as_view()" attached to the end to be recognised.


-- Clive


On 12 Nov 2020, at 04:35, George Olson wrote:

> I don't quite follow your note there of just going to:
> http://127.0.0.1:8000/
> without the app name 'polls' following.
>
> I believe you have to put the app name following that. It was  
> working before with http://127.0.0.1:8000/polls/, but then I did  
> something and tried to backtrack and the whole system got messed  
> up. I tried to reset everything back to the original but it is  
> still not working.
>
> On Wednesday, November 11, 2020 at 7:47:58 PM UTC-6 Clive Bruton  
> wrote:
>
> On 11 Nov 2020, at 23:37, George P. Olson wrote:
>
> > So when I go to this url: http://127.0.0.1:8000/polls/
>
> Although the urlpatterns below shows "# ex: /polls/", the defined
> path has no path elements, so you just need:
>
> http://127.0.0.1:8000/
>
> I think.
>
> > urlpatterns = [
> > # ex: /polls/
> > path('', views.index, name='index'),...
>
>
> -- Clive
>
>
> --
> You received this message because you are subscribed to the Google  
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it,  
> send an email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/ 
> d/msgid/django-users/1feb0c4a-8919-4aff-abb1-e0110392c396n%
> 40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6B7CE16C-69CD-4291-AC32-A81D54023029%40indx.co.uk.