0

I am trying my hand at learning Django and trying out the step-by-step tutorial at https://docs.djangoproject.com/en/2.0/intro/tutorial03/.

I have completed the app (well, till the Part 7) and is working as expected (and has been explained in the tutorial).

The only problem (so far) I am facing is when I am trying to navigate from the "Admin" page to the linked page "VIEW SITE" when I am being presented with "Page not found (404)" error. An image is being attached to make the situation clearer.

The link is pointing to "http://127.0.0.1:8000/" whereas it should be pointing to "http://127.0.0.1:8000/polls/". When I add the missing part of the path (manually) in the address bar the correct page (as expected) is presented.

I have tried to search on this as well as many other forums but could not get the right solution.

I am using Django 2.0.6 and Python 3.6.4 on mac sierra.

Shall be grateful for a lead on this.

Thanks

mysite/urls.py

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

mysite/polls/urls.py

from django.urls import path from . import views app_name = 'polls' 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'), ] 

polls/template/polls/index.html

{% load static %} <link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" /> {% if latest_question_list %} <ul> {% for question in latest_question_list %} <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li> {% endfor %} </ul> {% else %} <p>No polls are available.</p> {% endif %} 

Error on navigation at VIEW SITE

3
  • I don't really see how this is a problem. This is just a tutorial, and it so happens that it only defines URLs under /polls. Of course in a normal site you would have a home page at the base of the site, and that's what that admin link goes to. Commented Jun 29, 2018 at 10:05
  • @Daniel Roseman May be it's "just" a tutorial but going thru' (one of the most complete tutorials on the web), it doesn't seem to be the case of oversight. The admin site and the client end site are the ones probably what the author of the piece wanted to demonstrate. Commented Jun 29, 2018 at 11:07
  • @9769953 - The link is quite old and does not fit the current version of Django!! Commented Jun 29, 2018 at 11:09

3 Answers 3

1

you should open http://127.0.0.1:8000/polls/

not

http://127.0.0.1:8000/.

If you wanna use http://127.0.0.1:8000/ then your path should be

from django.urls import include, path urlpatterns = [ path('', include('polls.urls')), path('admin/', admin.site.urls), ] 
Sign up to request clarification or add additional context in comments.

Comments

0

It should be like this:

path('', include('polls.urls')), 

not like this:

path('polls/', include('polls.urls')) 

Because it should be the root url of your website

1 Comment

Thanks for the input. Yes it's working (i.e. the link "View Site" takes me to the page with Questions). I am treating this as correct for now. But I have a query yet. If I have followed the instructions on the tutorial to the T (I think), why is it that this particular code is failing. As I have just started out on Django stuffs are still denser for me. Even after browsing the net could not come up with a plausible reason why the code line with 'polls/' is failing, whereas the rest of the tutorial works perfectly. Shall be grateful for a lead on this. And thanks for your time.
0

Here is what I have done (may be not the most elegant solution but works just fine).

I have modified the "mysite/urls.py" file as shown:

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

This way I am able to access the "polls" page from both "View Site" link on the Django Admin page (url: "127.0.0.1:8000") as well as from link residing elsewhere (url: "127.0.0.1:8000/polls/").

Thanks for all the help.

PS. Visiting https://docs.djangoproject.com/en/2.0/topics/http/urls/ may be of help to learners like me.

Comments