Django error 404 2458 when requesting objects by id

I am following this video trying to learn Django. I have completed this tutorial and I had the same problem even when I followed the code to a T.

I am trying to get information about a model object displayed on the web-page when entering the id of the object directly in the url like http://localhost:8000/app/item/1/ or http://localhost:8000/app/item/2/ as the video shows (7:30 into the video). But when I try, I get this error:

enter image description here


Original code from video:

views.py:

from django.shortcuts import render
from django.http import HttpResponse
from .models import Book 


def index(request):
    return HttpResponse('Hello, world!')

def book_by_id(request, book_id):
    book = Book.objects.get(pk=book_id)
    return HttpResponse(f"Book: {book.title}, published on {book.pub_date}")

models.py:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=200)  
    pub_date = models.DateTimeField('date published')  

urls.py:

from django.utils import path
from . import views


urlpatterns = [
    path('', views.index, name='index'),
    path('book/<int:book_id>', views.book_by_id, name='book_by_id'),
]

My code:

views.py:

from django.shortcuts import render
from django.http import HttpResponse
from .models import Item


def index(request):
    return HttpResponse('Hello, world!')

def item_by_id(request, item_id):
    item = Item.objects.get(pk=item_id)
    return HttpResponse(f"Item: {item.title}, published on {item.datetime_found}")

models.py:

from django.db import models
from django.utils.timezone import now

class Item(models.Model):
    title = models.CharField(max_length=200)  # Short description of the item
    datetime_found = models.DateTimeField(default=now, blank=True)  # Date and time of when the item was found

urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('item/<int:item_id>', views.item_by_id, name='item_by_id'),
]

Project-level urls.py:

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

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

What am I not getting right about the GET-request? I feel like the changes I’ve made are minimal. And I have migrated everything correctly. (I think)

Answer

Use this

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

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

or you can remove ‘app’ from your url and make it look like this http://127.0.0.1:8000/item/1