enter image description here

models.py

from django.db import models from django import forms # Create your models here. class Articles(models.Model): title = models.CharField(max_length= 200) post = models.TextField() date = models.DateTimeField() img = models.ImageField(upload_to='',) def __unicode__(self): return self.title def __str__(self): return self.title return self.name def get_absolute_url(self): return "//" % (self.id) 

views.py

 from django.views import generic from .models import Articles class IndexView(generic.ListView): template_name = "news/posts.html" context_object_name = 'articles_list' def get_queryset(self ): return Articles.objects.all() 

urls.py

 from django.urls import path, include from django.views.generic import ListView, DetailView from django.conf.urls.static import static from RapterGames import settings from . models import Articles urlpatterns=[ path('', ListView.as_view(queryset=Articles.objects.all().order_by("-date")[:20],template_name="news/posts.html")), path('<int:pk/>', DetailView.as_view(model=Articles, template_name="news/post.html")) ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

posts.html

 {% extends "ShapeHtml/wrapper.html" %} {% block content %} {% for post in object_list %} {% for articles in articles_list %} <p><img src="{{ MEDIA_URL }}{{ articles.img.url }}"/></p> {% endfor %} <a href="/news/{{post.id}}"> <h3> {{post.title}} </h3> </a> <h5>{{post.date|date:"dmY"}}</h5> {% endfor %} {% endblock %} 

    1 answer 1

    It is logical that way. In django.views.generic.ListView when used in a template, the queryset can be accessed using object_list . You additionally have a view parameter set to context_object_name, so you have in the template a nested loop for the same objects. Fix your posts.html

     {% extends "ShapeHtml/wrapper.html" %} {% block content %} {% for article in articles_list %} <p><img src="{{ MEDIA_URL }}{{ article.img.url }}"/></p> <a href="/news/{{article.id}}"> <h3> {{article.title}} </h3> </a> <h5>{{article.date|date:"dmY"}}</h5> {% endfor %} {% endblock %}