Django 1.7.11:render_to_string() 出现了一个意外的关键字参数 'context'。

7 浏览
0 Comments

Django 1.7.11:render_to_string() 出现了一个意外的关键字参数 'context'。

有几个相关的问题,比如这个问题,但是答案似乎不起作用。\n我在views.py中有以下内容:\n

def pilab(request):
    return render(request, 'pilab.html',{'foo':'bar'})

\n运行这个并访问相应的URL会返回以下错误:\n

Traceback:
File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/data_nfs/opensurfaces2/server/home/views.py" in pilab
  31.     return render(request, 'pilab.html',{'foo':'bar'})
File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/shortcuts.py" in render
  50.     return HttpResponse(loader.render_to_string(*args, **kwargs),
Exception Type: TypeError at /pilab/
Exception Value: render_to_string() got an unexpected keyword argument 'context'

\n奇怪的是,删除context字符串会返回几乎相同的错误:\n

Traceback:
File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/data_nfs/opensurfaces2/server/home/views.py" in pilab
  31.     return render(request, 'pilab.html')
File "/data_nfs/opensurfaces2/venv/local/lib/python2.7/site-packages/django/shortcuts.py" in render
  50.     return HttpResponse(loader.render_to_string(*args, **kwargs),
Exception Type: TypeError at /pilab/
Exception Value: render_to_string() got an unexpected keyword argument 'context'

\n如何修复这个错误?

0