无法打印模板中视图功能返回的字典

时间:2016-06-21 17:32:25

标签: python django

我的视图函数返回一个字典。我想在我的模板中打印字典。但没有任何印刷品。它打印application / json而不是打印字典的值。任何人都可以帮我解决这个问题吗?

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>

2 个答案:

答案 0 :(得分:0)

在django模板中,您使用<xsl:template match="p"> <xsl:copy> <xsl:for-each-group select="node()" group-ending-with="text()[matches(., $SEnd)][matches(following-sibling::w[1], '^\p{Lu}')]"> <s> <xsl:apply-templates select="current-group()" /> </s> </xsl:for-each-group> </xsl:copy> </xsl:template> 访问字典:

.items

答案 1 :(得分:0)

如果res返回false,则您不会将form.is_valid()传递给模板响应。相反,您只是将表单传递回模板。

这很可能是因为您没有看到您的迭代被打印出来。

在django模板中,您需要.items而不是.iteritems

此外,您可以尝试将render_to_response功能更改为render功能。 (我更喜欢后者)。

return render(request, 'certificate_expiry/display.html', {'form':form,'res':res})