M
M
MalekBV2019-09-05 22:59:41
Django
MalekBV, 2019-09-05 22:59:41

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

Views.py
def anime_tag(request, tag_id):
  anime = Anime.objects.filter(tags__id=tag_id)
  return render (request, "index_tag.html", {'anime': anime})

urls.py
path("anime/<int:anime_id>/tag/<int:tag_id>", views.anime_tag, name='anime_tag')

sample
{% 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

2 answer(s)
S
szelga, 2019-09-06
@malekBV

judging by urls.py, it gives you 2 integers as input: anime_idand tag_id, while the representation views.anime_tagaccepts only tag_id. fix it urls.py.

E
Egor Kazantsev, 2019-09-05
@saintbyte

Go ahead and use the ready-made
https://djangopackages.org/grids/g/tagging/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question