Django传递字典参数至模板报错:不存在books_book.zonar列
The error you’re seeing means your database doesn’t have the zonar column for the books_book table, even though you added the zonar field to your Book model. This is a common Django issue when model changes aren’t synced to the database via migrations.
Step-by-Step Solution
1. Confirm Your Model Setup
First, double-check that your zonar field is correctly defined in the Book model (which it is in your code):
class Book(models.Model): title=models.CharField(max_length=100) zonar=models.CharField(max_length=20) # This field exists in the model book_id=models.IntegerField(default=10) image=models.ImageField() file=models.FileField(upload_to="cars",default="")
2. Sync Model Changes to the Database
Django uses migrations to update the database schema when you modify models. Run these commands in your project root:
Generate a migration file:
python manage.py makemigrations books
(Replace books with your app name if it differs.)
Apply the migration to update the database:
python manage.py migrate
This will create the missing zonar column in your books_book table.
Additional Fix: Template Typo
Once you resolve the column error, you’ll notice book titles still won’t display correctly. There’s a typo in your template:
<h1>{{ book.tile}}</h1> <!-- "tile" is a mistake -->
Change it to:
<h1>{{ book.title}}</h1>
Why This Happened
When you add a new field to a Django model, the database doesn’t automatically update. You need to generate a migration file that describes the change, then apply it to modify the database schema. Skipping these steps leads to "no such column" errors because the database has no knowledge of the new field.
内容的提问来源于stack exchange,提问作者sriram anush

