I've a strange problem in Django template. It is a template for show a list of the articles and for everyone of they I show a list of keyword that I've called key concepts.
The stranger thing is that instead of a list of key concepts it is shown a list of articles that use that key concept.
Below the E/R diagram and model and template of my project:
Models.py
class KeyConceptModel(models.Model):
concept_text = models.CharField(max_length=50)
def __str__(self):
return self.concept_text
def get_absolute_url(self):
return reverse("keyconceptManuscriptusView", kwargs={"pk": self.pk})
class Meta:
verbose_name = "Concetto chiave"
verbose_name_plural = "Concetti chiave"
class PostModel(models.Model):
post_title = models.CharField(max_length=70)
post_short_description = models.TextField(max_length=200)
post_contents = models.TextField()
post_publishing_date = models.DateTimeField(auto_now=False, auto_now_add=True)
post_author = models.ForeignKey(AuthorModel, on_delete=models.CASCADE)
post_keyconcept = models.ManyToManyField(KeyConceptModel)
slug = models.SlugField(verbose_name="Slug", unique="True")
post_highlighted = models.BooleanField(default=False)
def __str__(self):
return self.post_title
def get_absolute_url(self):
return reverse("singlepostManuscriptusView", kwargs={"slug": self.slug})
class Meta:
verbose_name = "Articolo"
verbose_name_plural = "Articoli"
Views.py
class ListPostGDV(ListView):
model = PostModel
template_name = "manuscriptus_home.html"
Template
{% for posts in object_list %}
<div id="news" class="container">
<div class="row">
<img class="img-fluid" src="{% static 'manuscriptus/img/demo_img.png' %}" alt="Header image">
</div>
<div class="row">
<div class="col-3">
<div class="row">
<small class="text-muted">Pubblicato il <strong>{{ posts.post_publishing_date|date }}</strong></small>
</div>
<div class="row">
{% for keyword in object_list.all %}
<p>{{ keyword }}</p>
{% endfor %}
</div>
</div>
<div class="col-9">
<div class="row">
<p class="h3"><a href="{{ posts.get_absolute_url }}">{{ posts.post_title }}</a></p>
</div>
<div class="row">
<p class="h5">{{ posts.post_short_description|safe|linebreaks }}</p>
</div>
</div>
</div>
</div>
{% empty %}
<div id="news" class="container">
<h1>Go to the admin panel and create your first post!</h1>
</div>
{% endfor %}
NB: I've used the generic detail views