Answer the question
In order to leave comments, you need to log in
How to make tags in Django?
I don’t know how to make tags in Django, it constantly displays errors, no matter how carefully I repeat after the video lesson / article. Apparently outdated methods were used, but in general. I have:
class Anime(models.Model):
# something code
class Tag(models.Model):
name = models.CharField(max_length = 25)
animes = models.ManyToManyField(Anime, related_name='tags')
def __str__ (self):
return self.name
def anime_tag(request, tag_id):
anime = Anime.objects.filter(tags__id=tag_id)
return render (request, "index_tag.html", {'anime': anime})
path("anime/<int:anime_id>/tag/<int:tag_id>", views.anime_tag, name='anime_tag')
{% for a in anime %}
Name: {{ a.title }}
Text: {{ a.content }}
Tags:
{% for tag in a.tags %}
<a href="{% url "anime_tag" tag.id %}">{{ tag.name }}</a>
{% endfor %}
{% endfor %}
Answer the question
In order to leave comments, you need to log in
judging by urls.py
, it gives you 2 integers as input: anime_id
and tag_id
, while the representation views.anime_tag
accepts only tag_id
. fix it urls.py
.
Go ahead and use the ready-made
https://djangopackages.org/grids/g/tagging/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question