pythondjangorequesthttp-status-code-404urlconf

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


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

Solution

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