gpt4 book ai didi

python - 呈现 : 'BoundField' object is not iterable 时捕获类型错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:20 25 4
gpt4 key购买 nike

我正在尝试将标签列表显示为 tag.name(而不是列表)。但是,当我尝试在列表上运行 for 循环时,它会抛出 "Caught TypeError while rendering: 'BoundField' object is not iterable."

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

遍历 .all 将加载页面,但不显示 Tags 字段。

  <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 时捕获类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4159400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com