Caught TypeError on rendering: BoundField object is not iterable

I am trying to display a list of tags as tag.name (instead of a list). However, when I try to start the for loop on the list, it throws a "Caught TypeError when rendering: the" BoundField "object is not iterable."

<dd>{% for tag in form.tags %}{{tag.name}}{% endfor %}</dd> 

Iterating through .all will load the page, but will not display the Tags field.

  <dd>{% for tag in form.tags.all %}{{tag.name}}{% endfor %}</dd> class Profile(models.Model): user = models.ForeignKey(User) tagging.register(Profile) form = ProfileForm(initial={ "fullname":fullname, "location":request.user.get_profile().location, "website":request.user.get_profile().website, "twitter_account":request.user.get_profile().twitter_account, "email":request.user.email, "bio":request.user.get_profile().bio, "tags":request.user.get_profile().tags }) class ProfileForm(forms.Form): fullname = forms.CharField( label=_("Full Name"), widget=forms.TextInput(), required=False) location = forms.CharField( label=_("Location"), widget=forms.TextInput(), required=False) website = forms.CharField( label=_("Website"), widget=forms.TextInput(), required=False) twitter_account = forms.CharField( label=_("Twitter"), widget=forms.TextInput(), required=False) bio = forms.CharField( label=_("Bio"), widget=forms.Textarea(), required=False) tags = forms.CharField( label=_("Keywords"), widget=forms.Textarea(), required=False) 

Thank you very much in advance!

+4
source share
3 answers

Code from 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) 
+1
source
 {% for tag in form.tags.choices %}{{tag.1}}{% endfor %} 
0
source

form.tags defined in the CharField form using the TextArea widget. Therefore, when you access form.tags , you get access to this field, and not to the base attribute of the tags model (which I assume is a kind of m2m).

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

0
source

All Articles