Page not found (404) Include not working

For some reason django cannot see my empty path string. It is acting as if include is not working. Any advice on fixing this?

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

  1. polls/
  2. admin/

The empty path didn’t match any of these.

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

polls/urls.py

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
]

polls/views.py

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

What is the url that returned a 404 ?

http://127.0.0.1:8000/

I ran python manage.py runserver

So, this is actually not a valid url for your project. The index page of your polls app is located at http://127.0.0.1:8000/polls/

Re-read carrefully the urls documentation at URL dispatcher | Django documentation | Django which explains that

At any point, your urlpatterns can “include” other URLconf modules. This essentially “roots” a set of URLs below other ones.

The keypoint here is that the polls app’s urls are “rooted” below the polls/ base url, hence the empty url from polls app correspond to the global polls/ url

I just got finished reading the documentation links that you sent me.

Based on my understanding I told Django that the “ ” situation would only apply if there was a polls/ url.

When I ran python manage.py runserver I wasn’t telling Django anything about polls/url and it gave me the error.

I think that’s what you are saying.

What is this global polls/url? Is there a local vs global thing going on?

So basically, the include method was for empty strings that had the path/ url tag prefixed.

“” by itself does not have the path/ and therefore it did not have a valuation for that and it failed.

What is this about a global polls url?

I know of python functions and scopes. Are we talking about global urls and local urls? I ma be a little confused there.

this is what the polls tutorial told me to do. I am lost as to why my code is different from there’s when I literally used the copy function.

If I create an empty " " what view would I route it to? How do I get passed this error?

Correct. You are following the tutorial. Nowhere at that stage of the tutorial does it tell you to go to that url. That is not a valid url. It’s not supposed to be at that point.

Right now, from what I see you’ve posted, everything is working exactly as intended.

I figured it out, it said run python manage.py run server but underneath it says go to the /polls tag. Okay perfect! :slight_smile: