不推荐使用render_to_string的context_instance参数

Seb*_*sen 7 django

我正在使用Django 1.9.

随着render_to_string我可以很容易地通过呈现的HTML作为JSON我的客户端脚本.但是,由于模板依赖于变量user,我还需要传递context_instance=RequestContext(request),否则模板不会知道是什么request.user,所以if语句中断等等.

但是我得到了这个弃用警告:

RemovedInDjango110Warning:不推荐使用render_to_string的context_instance参数.response_data ['content'] = render_to_string("profile/userprofile_detail/content.html",context,context_instance = RequestContext(request))

什么是非过时的方式来传递RequestContextrender_to_string

Say*_*yse 6

render_to_string 有一个上下文参数,所以你可以直接将它作为字典传递,就像你对任何其他响应一样

render_to_string(template_name, context=None, 
                 context_instance=_context_instance_undefined, request=None, using=None)
Run Code Online (Sandbox Code Playgroud)

链接的文档还包括一个鼓励这一点的说明

从1.8版开始不推荐使用:

不推荐使用context_instance参数.使用上下文和必要的请求.