class Main(models.Model):
link_name = models.CharField(max_length = 25)
topic_name = models.CharField(max_length = 100)
content = models.TextField()
tag_in_main = models.ForeignKey(Tag)
def __str__ (self):
return self.link_name
class Meta:
ordering = ["-id"]
class Tag(models.Model):
tag_main = models.CharField(max_length = 25, default=1)
def __str__ (self):
return self.tag_main
вьюшка
def tag(request):
tag = Main.objects.all()
content={
"tag":tag
}
return render (request, "index_tag.html", content )
хтмл
{% for obj in tag %}
<a href='{%url 'tag' id=obj.tag_main.id %}'>{{obj.tag_in_main}}</a><p>
{% endfor %}
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 %}
def postInTags(request, id):
tags = Tag.objects.filter().get(id=id)
posts = tags.post_set.all()
return render(request, 'tag_posts.html', {'posts': posts, 'tags': tags})