3DTagCloudwithXML 中使用的是xml生成方式 为了xml自动化生成 而不需要去手工修改xml 特地写了个xmlgenerator 比较粗糙 主要还是上一篇将的库导入以及环境变量的问题
具体代码如下:
#!/usr/bin/python from xml.etree import ElementTree from xml.etree.ElementTree import Element,SubElement,tostring from xml.dom import minidom import random #from django.conf import settings from sys import path path.extend(['/home/zoueswebsite','/home/zoueswebsite/blog','/home/zoueswebsite/zoueswebsite']) import os os.environ['DJANGO_SETTINGS_MODULE'] = 'zoueswebsite.settings' from blog.models import * def prettify(elem): rough_string = ElementTree.tostring(elem,'utf-8') parse_string = minidom.parseString(rough_string) return parse_string.toprettyxml(indent=" ") root = Element('tag') for num,value in enumerate(Category.objects.all()): SubElement(root,'a',{'href': '/category/'+value.alias,'class': "tag-link-" + str(Category.objects.count()),'title': "2 topics",'rel': "tag",'style': "font-size:"+ str(random.uniform(10,20))+"pt;",'color': "0xff0099",}) with open('category.xml','w') as f: f.write(prettify(root))