Search code examples
djangodjango-taggit

Django-taggit how to get all tags


I can get all tags' name one by one like this in template

{% for tag in blog.tags.all %}
    <span class="label label-default">{{ tag.name }}</span>
{% endfor %}

And I can get a input from a form like this {{ form.tags }} and it gives me:

<input id="id_tags" name="tags" type="text" value="xxx,y y y,zzz">

However I want to customize my input in my template like this

<input id="id_tags" class="form-control" maxlength="50" name="title" type="text" placeholder="tags" value="{{ form.tags }}">

How to set the input's value="{{ form.tags }}"?


Solution

  • In your model you can do the following:

    1. First you should have

      tags = TaggableManager(blank = True)

    2. the following will get all the tags and you can use get_tags in a list_field to get all tags for each record.

      def get_tags(self): tags = [] for tag in self.tags.all(): tags.append(str(tag)) return ', '.join(tags)