django生成json

好方便啊……list什么的一下都变成json了呢!

import json
from django.core.serializers.json import DjangoJSONEncoder

def main(request):
    output = []
    ucc = Userformulateplan.objects.all()
    for i in ucc:
        plan = {}
        plan['username'] = i.username.username
        plan['cname'] = i.pid.cid.cname
        plan['deadline'] = i.pid.deadline
        output.append(plan)
    t = loader.get_template("main.html")
    c = Context({"output" : json.dumps(output, ensure_ascii=False, cls=DjangoJSONEncoder)}) #第二个参数使中文字符不会被转成ascii码,第三个参数使日期可以正常转换,需要第二个import
    response = HttpResponse(t.render(c))
    return response

  

posted @ 2015-12-23 11:08  露儿大人  阅读(758)  评论(0编辑  收藏  举报