# models.py from django.db import models from taggit.managers import TaggableManager class MyObject(models.Model): title = models.CharField(max_length=100) content = models.TextField() tags = TaggableManager() #template.html {% for object in objects %}{{ object.title }}
{{ object.content }}
-
{% for tag in object.tags.all %}
- {{ tag.name }} {% endfor %}
Here is what the above code is Doing:
1. Create a model with a title and content field, and a TaggableManager.
2. In the template, loop through all the objects and display the title, content, and tags.
The TaggableManager is a custom manager that adds tagging functionality to a model.
The {% for tag in object.tags.all %} loop will loop through all the tags for each object.
The
- and
- tags will create an unordered list of tags.
You can also use the {% for object in objects %} loop to display all the objects that have a certain tag.
For example, if you want to display all the objects that have the tag “python”, you can do this:
{% for object in objects %}
{% if “python” in object.tags.all %}{{ object.title }}
{{ object.content }}
{% endif %}
{% endfor %}This will loop through all the objects and only display the ones that have the tag “python”.