Context_instance argument for render_to_string is deprecated

I am using Django 1.9.

With render_to_string I can easily pass the displayed html as json to my client script. However, since the template relies on variables like user , I also need to pass context_instance=RequestContext(request) , otherwise the template will not know what request.user , so if break, etc.

However, I get this failure warning:

RemovedInDjango110Warning: The context_instance render_to_string argument is deprecated. response_data ['content'] = render_to_string ("profile / userprofile_detail / content.html", context, context_instance = RequestContext (request))

What is an easy way to pass RequestContext to render_to_string ?

+6
source share
2 answers

render_to_string has a context argument, so you can just pass it directly as a dictionary, as with any other answer

 render_to_string(template_name, context=None, context_instance=_context_instance_undefined, request=None, using=None) 

Related documentation also contains a note to facilitate this.

Deprecated since version 1.8:

The context_instance argument is deprecated. Use context and, if necessary, query.

+6
source

The recommended method is to pass request when calling render_to_string . Then Django will display the template with the request context.

 render_to_string("profile/userprofile_detail/content.html", context, request=request) 
+2
source

All Articles