从Django中的列表创建标签云?

sup*_*er9 2 python django

我从API调用中获取了一个列表.我希望能够根据列表中的单词显示tagcloud.

我已经看到了一些其他可能的解决方案,在视图函数中生成HTML但如果我使用模板,我该如何绕过这一步?此外,使用django插件的解决方案似乎需要使用我觉得在我的情况下不需要的模型.

无论如何要做到这一点?

Hum*_*rey 8

有很多方法可以做到这一点.我个人会在你看来以这种格式操作标签列表:

tags = [
    { 'tag': 'django', 'size': 10 },
    { 'tag': 'python', 'size': 8 },
    { 'tag': 'Australia', 'size': 1 },
    { 'tag': 'coffee', 'size': 6 },
    { 'tag': 'pycon', 'size': 3 },
    { 'tag': 'html', 'size': 9 },
]
Run Code Online (Sandbox Code Playgroud)

在您的模板中:

<div class="tag-cloud">
    {% for t in tags %}
        <a href="/blog/tag/{{ t.tag }}/" class="size-{{ t.size }}">{{ t.tag }}</a> 
    {% endfor %}
</div>
Run Code Online (Sandbox Code Playgroud)

在你的CSS中:

.tag-cloud a.size-1 { font-size: 1.1em }
.tag-cloud a.size-2 { font-size: 1.2em }
.tag-cloud a.size-3 { font-size: 1.3em }
.tag-cloud a.size-4 { font-size: 1.4em }
.tag-cloud a.size-5 { font-size: 1.5em }
.tag-cloud a.size-6 { font-size: 1.6em }
.tag-cloud a.size-7 { font-size: 1.7em }
.tag-cloud a.size-8 { font-size: 1.8em }
.tag-cloud a.size-9 { font-size: 1.9em }
.tag-cloud a.size-10 { font-size: 2em }
Run Code Online (Sandbox Code Playgroud)