'字典'对象没有属性' push'

时间:2016-10-08 15:41:12

标签: django django-templates

我的views.py:

<Window ...
xmlns:local="clr-namespace:WpfStackOverflow"
...>
<DataGrid local:Window2.ContextMenuVisibilityMode="Single"  ... />

它给出了一个AttributeError:def my_view(request): if request.method == 'POST': #code else: form = #my_form context_dict = {} context_dict['myfield'] = 3 return render(request, 'core/my_template.html', context_dict, {'form': form}) #return render(request, 'core/my_template.html', {'form': form}, context_dict) #return render_to_response('core/my_template.html', context_dict, {'form': form}) #return render_to_response('core/my_template.html', {'form': form}, context_dict)

我找到this question,但这两种解决方案都不适合我。

下面这一行没有给出错误,但看起来没有传递任何内容('dict' object has no attribute 'push'没有打印任何内容)

{{ myfield }}

我正在使用django 1.9

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

您需要创建一个包含所有上下文变量的上下文字典,并将其作为第三个参数传递:

context_dict = {}
context_dict['myfield'] = 3
context_dict['form'] = form
return render(request, 'core/my_template.html', context_dict)