Django:我应该如何遍历 Taggit 标签并按字母顺序列出它们,同时在模板中将它们分开?
Django: How shouldI loop through Taggit tag's and list them alphabetically while at the same time seperating them in template?
实际上我在很多网站上都看到过这种情况,我想知道我将如何着手复制该功能。基本上标签遵循这种模式
A | all tags that start with A Y | all tags that start with Y
B | all tags that start with B Z | all tags that start with Z
A, B, ... Y, Z 可能会像 <h5> tag
和 <li> tags
中的所有标签
基本上我希望标签列表页面按字母顺序显示整个博客中使用的所有标签。
我知道怎么做,但可能不太实用。我正在考虑遍历所有标签,抓住每个项目的第一个字母,将其放入一个新词典中,然后循环遍历该词典以显示在我的模板中。
这会是一个实用的方法吗?
我也有第三方应用 taggit
和 https://github.com/fizista/django-taggit-templatetags2
这听起来像是视图可以更好地处理的事情。创建一个字典,其中键是字母表,值是相关标签的数组。将其传递给上下文。
Dict_you_want = {'A':['apple','argentina','aron'], 'B':['ball','banana'], ....,
. . . . . .. . . . .
'Z':['zebra','zeon']}
context['dict_you_want'] = Dict_you_want
进入 html 模板后,您可以遍历它并按照您想要的方式呈现它。
实际上我在很多网站上都看到过这种情况,我想知道我将如何着手复制该功能。基本上标签遵循这种模式
A | all tags that start with A Y | all tags that start with Y
B | all tags that start with B Z | all tags that start with Z
A, B, ... Y, Z 可能会像 <h5> tag
和 <li> tags
基本上我希望标签列表页面按字母顺序显示整个博客中使用的所有标签。
我知道怎么做,但可能不太实用。我正在考虑遍历所有标签,抓住每个项目的第一个字母,将其放入一个新词典中,然后循环遍历该词典以显示在我的模板中。
这会是一个实用的方法吗?
我也有第三方应用 taggit
和 https://github.com/fizista/django-taggit-templatetags2
这听起来像是视图可以更好地处理的事情。创建一个字典,其中键是字母表,值是相关标签的数组。将其传递给上下文。
Dict_you_want = {'A':['apple','argentina','aron'], 'B':['ball','banana'], ....,
. . . . . .. . . . .
'Z':['zebra','zeon']}
context['dict_you_want'] = Dict_you_want
进入 html 模板后,您可以遍历它并按照您想要的方式呈现它。