[英]Not able to print the dictionary returned from view function in template
my view function returns a dictionary . 我的视图函数返回一个字典。 I am trying to print the dictionary in my template.
我正在尝试在模板中打印字典。 But nothing gets printed.
但是什么也没印出来。 It prints application/json instead of printing the values of dictionary .Can anyone help me solve this ?
它打印application / json而不是输出dictionary的值。有人可以帮助我解决这个问题吗?
def display(request):
if request.method == 'POST' or 'GET' :
form = displayForm(request.POST or request.GET or None)
val = request.POST.get('ser')
form.fields['ser'].choices = [(val, val)]
if form.is_valid():
#context = RequestContext(request)
grp = request.POST.get('grp_name')
env1 = request.POST.get('env')
serv = request.POST.get('ser')
res ={}
res = check_expiry(request.GET,grp,env1,serv)
print res
return render_to_response('certificate_expiry/display.html', {'form':form ,'res': res} )
else:
form = UrlForm()
return render(request,'certificate_expiry/display.html',{'form':form})
## the html template
<div>
{% for k,v in res.iteritems %}
<li> {{ v }} </li>
{% endfor %}
</div>
In django template you use .items
to access dictionary: 在Django模板中,您使用
.items
访问字典:
{% for k,v in res.items %}
<li> {{ v }} </li>
{% endfor %}
You aren't passing res
to the template response if form.is_valid()
returns false. 如果
form.is_valid()
返回false, form.is_valid()
res
传递给模板响应。 Instead you are only passing the form back to the template. 相反,您仅将表单传递回模板。
This is most likely why you aren't seeing your iteration get printed out. 这很可能是您看不到迭代被打印出来的原因。
You need .items
rather than .iteritems
in django templates. Django模板中需要
.items
而不是.iteritems
。
Also, you can try changing your render_to_response
function into a render
function. 另外,您可以尝试将
render_to_response
函数更改为render
函数。 (I prefer the latter). (我更喜欢后者)。
return render(request, 'certificate_expiry/display.html', {'form':form,'res':res})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.