[Django]-Caught TypeError while rendering: 'BoundField' object is not iterable

1👍

Code from a Howto Post


Template:

{% for tag in blogpost.get_tags %}
  <a href="/blog/tag/{{tag}}" alt="{{tag}}" title="{{tag}}">{{tag}}</a>
{%endfor%}


Model:

from django.db import models
from tagging.fields import TagField
from tagging.models import Tag

class BlogPost(models.Model):

    title = models.CharField(max_length=30)
    body = models.TextField()
    date_posted = models.DateField(auto_now_add=True)
    tags = TagField()

    def set_tags(self, tags):
        Tag.objects.update_tags(self, tags)

    def get_tags(self, tags):
        return Tag.objects.get_for_object(self)      

0👍

{% for tag in form.tags.choices %}{{tag.1}}{% endfor %}

0👍

form.tags is defined in your form as a CharField with a TextArea widget. So when you access form.tags you are accessing that field, not the underlying tags model attribute (which I assume is some sort of m2m).

Without seeing your models it’s impossible to tell you exactly how to achieve what you’re trying to do, but the general idea is that you need to iterate over the tag objects, not the form field.

Leave a comment