我试图将标签列表显示为 tag.name (而不是列表)。但是,当我尝试在列表上运行 for 循环时,它会抛出 “渲染时捕获的类型错误:'BoundField' 对象不可迭代。”
<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)
提前谢谢了!
最佳答案
来自 Howto Post 的代码
模板:
{% for tag in blogpost.get_tags %}
<a href="/blog/tag/{{tag}}" alt="{{tag}}" title="{{tag}}">{{tag}}</a>
{%endfor%}
模型:
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)
关于python - 渲染 : 'BoundField' object is not iterable 时捕获 TypeError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4159400/