pass multiple objects to RequestContext in django

不问归期 提交于 2020-06-25 21:34:55

问题


I need to pass a dict and an object to a template. So, I do this

rc = RequestContext(request, {'prob':prob}, {'result':result})
return render_to_response('subject/question.html', context_instance=rc)

But I get a error.

Traceback: File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  111.  response = callback(request, *callback_args, **callback_kwargs) File "E:\django-sample\proschools\..\proschools\subject\views.py" in eval_c
  72.   rc = RequestContext(request, {'prob':prob}, {'result':result}) File "C:\Python27\lib\site-packages\django\template\context.py" in __init__
  173.  self.update(processor(request))

Exception Type: TypeError at /practice/c/eval/ 
Exception Value: 'str' object is not callable

回答1:


rc = RequestContext(request, {'prob':prob, 'result':result})

3rd parameter is processors that should be tuple or list




回答2:


You can also use the render shortcut which includes RequestContext and then just pass your variables normally.

from django.shortcuts import render

def someview(request):
    return render(request,'subject/response.html',{'prob':prob,'result':result})


来源:https://stackoverflow.com/questions/10361056/pass-multiple-objects-to-requestcontext-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!