This is a simple template tag to create Tag Clouds. Based on the number of Posts (change the model_name according to your schema), Tags are provided different size ( most number of posts => most popular => and hence the largest.
We create a property called cloudsize for each tag, which is an integer between minsize and maxsize.
Check the example of sample template use here http://www.djangosnippets.org/snippets/472/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | from django.template import Library, Node
from project.app.models import Tag
import random
register = Library()
maxsize =220 # maximum size of the most popular tag
minsize = 55 # minimum size of the least popular tag
class LatestTagsNode(Node):
def gen_clouds(self):
p=Tag.objects.all()
max1=max([int(p-item.video_set.count()) for p-item in p])
for i in range(p.count()):
size =int(round(int(p[i].video_set.count())*maxsize/max1))
if size<minsize:
size=minsize
cloudsize =str(size) +"%"
p[i].cloudsize=cloudsize
return p
def render(self, context):
self.__init__()
context['content_tagclouds'] = self.gen_clouds()
return ''
def get_latest_cloudtag(parser, token):
return LatestTagsNode()
get_latest_cloudtag= register.tag(get_latest_cloudtag)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.