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

2024-09-26 17:49:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从API调用中获取列表。我想能够显示一个基于列表中的单词的tagcloud。在

我已经看到一些其他可能的解决方案,在视图函数中生成HTML,但是如果我使用模板,我如何绕过这一步呢?另外,像使用django plugin的解决方案似乎需要使用模型,而我觉得在我的案例中没有必要使用这些模型。在

不管怎样要这么做?在


Tags: django函数模型视图模板api列表html
2条回答

迭代模板中的标记列表,并将“random”过滤器用于内联字体大小样式的整数列表。在

http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#random

视图.py

...
sizes = [10, 12, 14, 16, 18, 20]
tags = ["one", "two", "three"]
...

索引.html

^{pr2}$

有很多方法可以做到这一点。就个人而言,我会在您的视图中将标记列表处理成以下格式:

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 },
]

在模板中:

^{pr2}$

在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 }

相关问题 更多 >

    热门问题