Skip to content
Advertisement

Django Error: Field ‘id’ expected a number but got ‘list’

I have an app with 2 models and I’m trying to create a List View and a Detail View. The list view worked fine, but when I created the detail view the list view stopped working and it’s prompting me with this error: “Field ‘id’ expected a number but got ‘list’.

Models.py

JavaScript

views.py

JavaScript

urls.py

JavaScript

and my templates:

JavaScript
JavaScript

Advertisement

Answer

Your pattern ^(?P<pk>[-w]+)/$ matches list hence the view SchoolDetailView gets used for that url (in fact also for the other urls after it), hence you need to make the pattern more specific. For instance the pk is going to be an integer only so you can match that instead:

JavaScript

Better yet this doesn’t need you to use re_path does it? the default int path converter handles it quite well:

JavaScript

Note: Class names should ideally be in PascalCase and also model names should be singular so instead of schools a better name for the model would be School, instead of students it would be Student. See PEP 8 — Style Guide for Python Code

Advertisement