【发布时间】:2011-05-08 17:55:48
【问题描述】:
我正在尝试将标签列表显示为 tag.name(而不是列表)。但是,当我尝试在列表上运行 for 循环时,它会抛出 "Caught TypeError while rendering: 'BoundField' object is not iterable."
<dd>{% for tag in form.tags %}{{tag.name}}{% endfor %}</dd>
遍历 .all 将加载页面,但不显示标签字段。
<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)
非常感谢!
【问题讨论】:
-
您是否尝试过在 django shell 中加载 ProfileForm 并查看
form.tags.all()返回的内容? -
我没有。我对 django shell 不是很有经验。那么从表单导入 ProfileForm ... 那么呢?
-
from forms import ProfileForm然后form = ProfileForm(initial={blahblahblah...})然后form.tags.all()看看它输出了什么 -
>>> form = ProfileForm(initial={'fullname': 'emile', 'tags': {'shoe','cheese'}}) >>> form.tags.all( )回溯(最近一次调用最后):文件“
”,第 1 行,在 AttributeError:'ProfileForm' 对象没有属性'tags' -
最后一件我忘记了,我正在使用 Django-Tagging 应用程序:code.google.com/p/django-tagging
标签: python django django-forms django-views