我KeyError (Exception Value: 'request)
在使用模式对话框和使用 Ajax 时遇到了问题。这是我的设置:
当有人点击一个链接时,会有一个 ajax 调用运行一个函数来查询一个名为 part 的对象:
<script type="text/javascript">
$(document).ready(function() {
$(document).on("click", ".lib", function () {
var id = $(this).attr("id");
$.get("/models/"+id+"/viewmodelinfo/", function(data) {
$(".modal-body").html(data);
$('#mySymModal').modal('show');
});
});
});
</script>
这是查询零件对象的函数:
def viewmodelinfo(request, model_id):
part = Part.objects.get(id=model_id)
return render_to_response("models/viewmodelmodal.html", {"part": part })
这会发回 html 数据,这些数据在modal-body
我的模态对话框的 div 中被替换。
这一切都很好,除非我在viewmodelmodal
模板中有以下代码行:
{% comments part %}
在这种情况下,我得到一个KeyError
.
这是回溯:
追溯:
File "/Users/user1/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/user1/Desktop/store/models/views.py" in viewmodelinfo
467. return render_to_response("models/viewmodelmodal.html", {"part": part })
File "/Users/user1/Desktop/myenv2/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/Users/user1/Desktop/myenv2/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
171. return t.render(Context(dictionary))
File "/Users/user1/Desktop/myenv2/lib/python2.7/site-packages/django/template/base.py" in render
140. return self._render(context)
File "/Users/user1/Desktop/myenv2/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
62. return self.nodelist.render(context)
File "/Users/user1/Desktop/myenv2/lib/python2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Users/user1/Desktop/myenv2/lib/python2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Users/user1/Desktop/myenv2/lib/python2.7/site-packages/django/template/base.py" in render
1178. _dict = func(*resolved_args, **resolved_kwargs)
File "/Users/user1/Desktop/store/apps/threadedcomments_extras/templatetags/comments_tag.py" in comments
13. "request": context["request"],
File "/Users/user1/Desktop/myenv2/lib/python2.7/site-packages/django/template/context.py" in __getitem__
54. raise KeyError(key)
Exception Type: KeyError at /models/384/viewmodelinfo/
Exception Value: 'request'
此评论在我使用它的其他任何地方都可以正常工作。这使我认为我应该包括一些我缺少的东西。有任何想法吗?