user5076666
user5076666

Reputation:

Writing my first Django app

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

Upvotes: 0

Views: 236

Answers (3)

user5076666
user5076666

Reputation:

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.

Upvotes: 0

vipul gangwar
vipul gangwar

Reputation: 313

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

Upvotes: 1

Chams Agouni
Chams Agouni

Reputation: 374

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

Upvotes: 0

Related Questions