无法访问django模板中的dict列表

时间:2013-04-15 11:53:19

标签: python django list dictionary

我的视图中有一个列表,如

getit = [{u'company': u'',
  u'customerId': u'qwert',
  u'dateCreated': u'2012-10-28 08:52:43.0',
  u'dateModified': u'2012-12-17 22:27:58.0',
  u'email1': u'',
  u'email2': u'',
  u'extensionId': u'306',
  u'message': u'',
  u'name': u'',
  u'phone1': u'0284533',
  u'phone2': u'',
  u'productCode': u'12400',
  u'prospectId': u'795',
  u'status': u'0',
  u'subscriberEmail': u'naveen@hi.com',
  u'subscriberId': u'ro@gmail.com',
  u'title': u'',
  u'trackId': u'12345',
  u'vendorEmail': u'va@tal.com',
  u'vendorNumber': u'0284533'}]

context['hi'] = getit

我试图在我的模板中迭代这些数据,如。

{% for x in hi %}
   {{x.status}}
   {{x.productCode}}
{% endfor %}

很遗憾无法在我的模板中访问

请帮我解决我在这里做错的事情

* 已更新*

这是我的观点

def  subscriberreport(request):
    context = {}

    subscriberId = request.session['subscriberId']
    REST_URL = 'http://10.10.10.76:8100/UCSRestService/prospectrequest/' + subscriberId + '/subscriber'
    context = {}
    geta = Agent()
    getr = geta.GETT('caason',REST_URL,'apikey','SecretString')
    geti = geta.response.text
    getwj = json.loads(geti)
    for i,j in getwj.items():
        if i == "data":
           getit = j
    print getit
    print type(getit)
    context['hi'] = getit

    RESTC_URL = 'http://10.10.10.76:8100/UCSRestService/calldetailrecord/' + subscriberId + '/subscriber'
    context = {}
    getac = Agent()
    getrc = getac.GETT('caason',RESTC_URL,'apikey','SecretString')
    getic = getac.response.text
    getwjc = json.loads(getic)
    print "++++++++++++++++++++++CDR+"

    for k,v in getwjc.items():
        if k == "data":
           context['cdr'] = v
        if k == "error":
           context['cdr'] = False
    context['report'] = True
    return render_to_response('actual/reports.html', context, context_instance=RequestContext(request))

2 个答案:

答案 0 :(得分:2)

在向其添加getit后,您正在将上下文重置为空:

context['hi'] = getit

...

context = {}    # here's your problem

答案 1 :(得分:1)

只需删除以下行:

context = {}

你会没事的。 (此行清除上下文字典,因此您的数据将被删除)

旁注,你的代码很糟糕,我建议清理它。