This error appears when displaying the template:

Tell me how you can fix this error.

models.py :

 from django.db import models from django.utils import timezone from PIL import Image class Post(models.Model): author = models.ForeignKey('auth.User') title = models.CharField(max_length=200) text = models.TextField() created_date = models.DateTimeField( default=timezone.now) published_date = models.DateTimeField( blank=True, null=True) image = models.ImageField(blank=True, upload_to='images') def publish(self): self.published_date = timezone.now() self.save() def __str__(self): return self.title 
  • Add the missing column in the table in the database - andreymal
  • @andreymal, how can you do this? - Kek
  • And for this you need to know what you did with the models. Probably, you once added an image to a post model and forgot to create and execute a migration? - andreymal
  • @andreymal, supplemented his question with the file models.py. please tell me what kind of migration are you talking about? - Kek

1 answer 1

The problem is not in the template. Judging by the screenshot, your posts variable is a lazy QuerySet, which climbs to the database for posts only at the first access to it, and it turned out that for post in posts in the template turned out to be this very first call. As a result, the template accesses the database, and this appeal falls because of the missing column in the table, which was registered in the model.

Models only describe how tables and columns and their associated data should be interpreted, which are stored in the database, but do not manage these tables and columns in the database in any way. If a field has been added or deleted in the model, the corresponding column will not be automatically changed in the database itself. In general, it is impossible to do this automatically for various technical and logical reasons, but it is semi-automatically possible - for this there are migrations.

The brief point is this: when changing a model, we run makemigrations , junga looks at changes in the model and tries to guess what changes need to be made in the database (most often it guesses correctly, but not always); The migration is saved to a file, after which we modify the database itself according to the instructions in this migration by running the migrate command.

Details about all this in the official documentation: https://djbook.ru/rel1.9/topics/migrations.html (translation)

  • The template does not access the database. For example, the database is accessed by functions from view - Mikhail Alekseevich
  • @ MikhailAlekseevich appeals. The variable posts is a QuerySet object that is lazy and only takes objects from the database the first time it is accessed. Accordingly, the for post in posts loop from the template turns out to be this very first call and thereby provokes a request to the database, which drops. (Is it you who put minuses without understanding how Django works?) - andreymal
  • I can easily give an example of code, when the first access to the database will occur even in view. I agree that this is not always optimal, but there is no view function in the question, and it is unequivocal to say that you always cannot work as written - Mikhail Alekseevich
  • @ MikhailAlekseevich yes, there are many different cases when QuerySet will get into the database even in view (I myself often twitch it in view so that it is guaranteed not to render the database when rendering the template). Still, in this particular case, he climbed into the database during the rendering of the template) This can be uniquely said from the screenshot presented in the question. - andreymal
  • However, I added a clarification in response to avoid misunderstandings in the future - andreymal