[英]How to display django tags in update form (django-taggit)?
我在 django 中制作了一個基本的博客文章應用程序,我正在使用 django-taggit 項目( https://github.com/jazzband/django-taggit )來創建可標記的 ZA559B87068921EEC05086ECE 對象但是,標簽在我的更新表單字段中顯示為查詢集:
<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
我沒有保存標簽的問題。 我唯一的問題是在我的更新表單的可編輯字段中顯示已經存在的標簽。
謝謝!
forms.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',]
post.tags.all
是一個查詢集,因此它不會被評估,因為 django 查詢是惰性的,您只需獲取查詢集,因為它返回一組數據(如果需要,則為數組)而不是值。 嘗試這個:
<input type="text" name="tags" data-role="tagsinput" class="form-control" id="tags" name="tags" value="{% for tag in post.tags.all %}{{ tag }},{% endfor %}">
# I used a comma to separate them but feel free to use whatever you want
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.