How do I fix this error, thanks for any help.
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/list/5/
Django Version: 4.1.2
Python Version: 3.10.6
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'todo_app']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/bieehoang/AboutMe/Python/Projects/bamboo_polls/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/bieehoang/AboutMe/Python/Projects/bamboo_polls/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/bieehoang/AboutMe/Python/Projects/bamboo_polls/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/home/bieehoang/AboutMe/Python/Projects/bamboo_polls/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch
return handler(request, *args, **kwargs)
File "/home/bieehoang/AboutMe/Python/Projects/bamboo_polls/venv/lib/python3.10/site-packages/django/views/generic/list.py", line 154, in get
self.object_list = self.get_queryset()
File "/home/bieehoang/AboutMe/Python/Projects/bamboo_todo/todo_app/views.py", line 14, in get_queryset
return TodoItem.objects.filter(todo_list_id=self.kwargs["id"])
Exception Type: KeyError at /list/5/
Exception Value: 'id'
/home/bieehoang/AboutMe/Python/Projects/bamboo_todo/todo_app/migrations/0001_initial.py
Generated by Django 4.1.2 on 2022-10-16 10:38
from django.db import migrations, models
import django.db.models.deletion
import todo_app.models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='ToDoList',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100, unique=True)),
],
),
migrations.CreateModel(
name='TodoItem',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('description', models.TextField(blank=True, null=True)),
('created_date', models.DateTimeField(auto_now_add=True)),
('due_date', models.DateTimeField(default=todo_app.models.one_week_hence)),
('todo_list', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='todo_app.todolist')),
],
options={
'ordering': ['due_date'],
},
),
]
# /home/bieehoang/AboutMe/Python/Projects/bamboo_todo/todo_app/models.py
from django.db import models
from django.utils import timezone
from django.urls import reverse
def one_week_hence():
"""
Return 7 days after at that time create
"""
return timezone.now() + timezone.timedelta(days=7)
class ToDoList(models.Model):
title = models.CharField(max_length=100, unique=True)
def get_absolute_url(self):
"""
Return the URL for the particular data item
"""
return reverse('list', args=[self.id])
def __str__(self):
"""
Standard Python way of creating a readable
repersentation of an object
"""
return self.title
class TodoItem(models.Model):
title = models.CharField(max_length = 100)
description = models.TextField(null=True, blank=True)
created_date = models.DateTimeField(auto_now_add = True)
due_date = models.DateTimeField(default = one_week_hence)
todo_list = models.ForeignKey(ToDoList, on_delete = models.CASCADE)
def get_absolute_url(self):
""""""
return reverse(
'item-update', args = [str(self.todo_list.id), str(self.id)]
)
def __str__(self):
return f"{self.title}: due {self.due_date}"
class Meta:
"""
Default ordering for ToDoItem record
"""
ordering = ["due_date"]
/home/bieehoang/AboutMe/Python/Projects/bamboo_todo/todo_app/views.py
from django.shortcuts import render
from django.views.generic import ListView
from .models import ToDoList, TodoItem
class ListListView(ListView):
model = ToDoList
template_name = 'todo_app/index.html'
class ItemListView(ListView):
model = TodoItem
template_name = "todo_app/todo_list.html"
def get_queryset(self):
return TodoItem.objects.filter(todo_list_id = self.kwargs["id"])
def get_context_data(self):
context = super().get_context_data()
context["todo_list"] = ToDoList.objects.get(id = self.kwargs["id"])
return context
Then I got KeyError at /list/5/ when click to url from template to access ItemListView, can anyone tell me detail’s what I wrong and how do I fix this error? Thank everyone.