我在 django 中制作了一个基本的博客文章应用程序,并且正在使用 django-taggit 项目 ( https://github.com/jazzband/django-taggit ) 创建可标记的模型对象。但是,标签在我的更新表单字段中显示为查询集:
<QuerySet[<Tag:wow]>
这是我的 html 的样子:
<input type="text" name="tags" data-role="tagsinput" class="form-control" id="tags" name="tags" value="{{ post.tags.all }}">
我知道有一种方法可以在显示标签时循环它们,但是有没有办法在表单中循环它们?我正在使用单个文本字段来添加使用本教程以逗号分隔的标签:
https://dev.to/coderasha/how-to-add-tags-to-your-models-in-django-django-packages-series-1-3704
我没有保存标签的问题。我唯一的问题是在我的更新表单的可编辑字段中显示已经存在的标签。
谢谢!
表格.py:
from taggit.forms import TagWidget
class PostForm(ModelForm):
class Meta:
model = Post
widgets = {'content_text': forms.Textarea(attrs={'cols': 80, 'rows': 80}),
'tags': TagWidget(),
}
fields = ['title', 'video_URL', 'content_text', 'score', 'tags',]