Содержимое models.py
from django.db import models class Post(models.Model): title = models.CharField(max_length=150, db_index=True) slug = models.CharField(max_length=150, unique=True, blank=True) body = models.TextField(blank=True, db_index=True) date_pub = models.DateTimeField(auto_now_add=True) category = models.ForeignKey('Category', on_delete=models.CASCADE, related_name='posts') tags = models.ForeignKey('Tag', on_delete=models.CASCADE) def __str__(self): return self.title class Category(models.Model): title = models.CharField(max_length=150, db_index=True) slug = models.CharField(max_length=150, unique=True, blank=True) def __str__(self): return self.title class Tag(models.Model): title = models.CharField(max_length=150, db_index=True) slug = models.CharField(max_length=150, unique=True, blank=True) def __str__(self): return self.title
содержимое views.py
from django.views.generic import View from django.shortcuts import render from .models import * def news_list(request): posts = Post.objects.all() context = {'posts': posts} return render(request, 'news/post/news_list.html', context) def tags_list(request): tags = Tag.objects.all() context = {'tags': tags} return render(request, 'news/tag/tags_list.html', context) class Tag_Detail(View): def get(self, request, slug): tag = Tag.objects.get(slug__iexact=slug) context = {'tag': tag} return render(request, 'news/tag/tag_detail.html', context)
и сообственно сам шаблон где и происходит вызов ‘tag’
{% block content %} {% for post in tag %} {{ post.title }} {% endfor %} {% endblock content %}
Возможно что проблема из за models.ForeignKey('Tag', on_delete=models.CASCADE)? В джанго новичек и ForeignKey немного не понимаю.