Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django模板中无法访问Python字典_Django_Django Templates - Fatal编程技术网

django模板中无法访问Python字典

django模板中无法访问Python字典,django,django-templates,Django,Django Templates,我想使用从视图传递到模板的键访问字典值。虽然经过数小时的搜寻和挣扎,我仍然无法预测原因。下面是图片 views.py中的post方法 def post(self, request): value = request.POST['value'] if value is '': return redirect('home') else: start_time = time.time()

我想使用从视图传递到模板的键访问字典值。虽然经过数小时的搜寻和挣扎,我仍然无法预测原因。下面是图片

views.py中的post方法

    def post(self, request):
        value = request.POST['value']
        if value is '':
            return redirect('home')
        else:
            start_time = time.time()
            obj = CalcClass(value)
            result=obj.calculate()
            end_time = time.time() - start_time
            output={}
            output['result']=result
            output['end_time']=end_time

            return render(request, 'fibohome/home.html', output)
模板home.html


{%if输出%}
输出
{{output.result}
所需时间
{{output.end_time}
{%else%}
没有一个
{%endif%}

在传递给模板的内容中没有输出这样的内容
output
只是您为上下文词典指定的名称。模板接收该词典的内容。例如,您可以直接执行
{{result}
{end\u time}}

在post中添加键值对dict,这就是为什么它在模板中不可访问的原因。如果您使用get request,则模板可以访问它。请更正views.py中的最后一行,如下
return render(request,'fibohome/home.html',{'output':output})
@Paandittya这是正确的答案,您应该发布我Daniel,我尝试了您的解决方法,但不幸的是它没有起作用。尽管我是在@Paandittya上面的评论的帮助下完成的。下面是我为得到想要的结果所做的。在views.py
中返回render(请求'fibohome/home.html',{'output':output})
在template home.html
{{output.result}}
中是否删除了
{%if output%}
检查?否。如果阻塞仍然存在,那就是为什么;正如我所说,
输出
不存在。是的。它也按你的方式工作!但我需要if块。非常感谢您的时间和建议。