class Main(models.Model):
link_name = models.CharField(max_length = 25)
topic_name = models.CharField(max_length = 100)
content = models.TextField()
def __str__ (self):
return self.link_name
class Meta:
ordering = ["-id"]
class Tag(models.Model):
name = models.CharField(max_length = 25, default=1)
main = models.ManyToManyField(Main, related_name='tags')
def __str__ (self):
return self.name
def main_tag(request, tag_id):
main = Main.objects.filter(tags__id=tag_id)
content={
"main": main
}
return render (request, "index_tag.html", content )
...
url(r'^main/(\d+)/$', main_tag, name='main_tag'),
...
{% for m in main %}
Name: {{ m.topic_name }}
Text: {{ m.content }}
Tags:
{% for tag in m.tags %}
<a href="{% url "main_tag" tag.id %}">{{ tag.name }}</a>
{% endfor %}
{% endfor %}