How to fix "Page not found (404)" error ("Django tried these URL patterns... The empty path didn't match any of these.")

10.7k views Asked by At

As a newbie to Django, I encountered the same problem as many before me. I would appreciate if you didn't mark my question as a double immediately because I checked the fixes those old posts suggested but to no avail.

I was following this tutorial and have finished with all up to the heading "Projects App: Templates". Now when I start the server, at http://localhost:8000/ I get:

Page not found (404) Request Method: GET Request URL: http://localhost:8000/

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

admin/
projects/

The empty path didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

This is console output when I run server:

System check identified no issues (0 silenced).

April 05, 2019 - 15:31:54

Django version 2.2, using settings 'personal_portfolio.settings'

Starting development server at http://127.0.0.1:8000/

Quit the server with CTRL-BREAK.

Not Found: /

[05/Apr/2019 15:32:01] "GET / HTTP/1.1" 404 2042

Not Found: /favicon.ico

[05/Apr/2019 15:32:01] "GET /favicon.ico HTTP/1.1" 404 2093

What I tried, but didn't help:

  1. restarting the server,
  2. checking my code inside the files against the tutorial's source code,
  3. Made sure that 'projects' is inside the INSTALLED_APPS list in settings.py.

Here is urls.py that's inside rp-portfolio\personal_portfolio:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('projects/', include('projects.urls'))
]

Here is urls.py that's inside rp-portfolio\projects:

from django.urls import path
from . import views

urlpatterns = [
    path("", views.project_index, name="project_index"),
    path("<int:pk>/", views.project_detail, name="project_detail"),
]
5

There are 5 answers

0
Hari On BEST ANSWER

In the urls.py

from django.urls import path
from django.views.generic import RedirectView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('projects/', include('projects.urls')),
    path('', RedirectView.as_view(url='/projects/')),
]
0
rahul.m On

try this

in urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('projects.urls'))
]

hope it helps

0
Utkarsh Singh On

as you are seeking a path inside project your requested path must be this http://localhost:8000/projects/

0
samu On

You need to add:

in polls/urls.py

add this: from django.conf.urls import url

so it shold look like:

from django.urls import path
from django.conf.urls import url
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]
0
devDavmen On

Sometimes the URL picks up other words from the terminal.

Example 1.0

Starting development server at http://127.0.0.1:8000/Quit the server with CTRL-BREAK.

For instance in Example 1.0, instead of opening a link like http://127.0.0.1:8000/

The link will be http://127.0.0.1:8000/Quit since /Quit is not included as part of your code you will get the 404 error.

Example 2.0

Starting development server at http://127.0.0.1:8000/
Quit the server with 
CTRL-BREAK.
Not Found: /Quit
[27/May/2021 22:36:07] "GET /Quit HTTP/1.1" 404 2066
[27/May/2021 22:36:59] "GET / HTTP/1.1" 200 5873
[27/May/2021 22:37:04] "GET / HTTP/1.1" 200 5873

Hope you find this helpful.