1👍
You are currently retrieving Spots
that have the same primary key as the ArticleSpots
object, but that does not make much sense: it is possible that this is the case, but even if that happens, the returned Spots
does not per se is linked to a relevant ArticleSpots
with the given article.
You can retrieve the relevant Spots
with:
def article(request, slug):
article = get_object_or_404(Articles, slug=slug)
spots = Spots.objects.filter(articlespots__article=article)
context = {'spots': spots, 'article': article}
return render(request, 'articletemplate.html', context)
I would strongly advise to name you Article
object article
since it is a single Article
, not a collection of Article
s. spots
on the other hand is a collection of spots.
It makes no sense to render {{ spots.content1 }}
and {{ spots.title }}
, since spots
is a collection of Spots
that can contain zero, one or more items.
The template thus should look like:
<p>
{{ article.title }}
</p>
{% for spot in spots %} {{ spot.title}} {% endfor %}
Note: normally a Django model is given a singular name, so
Articles
instead of.Article