Fixed #25854 -- Removed deprecated usage of template.render() with RequestContext in docs.

This commit is contained in:
Alasdair Nicol 2015-12-23 21:14:27 +00:00 committed by Tim Graham
parent 5081adcb90
commit 32c7d93e5f
2 changed files with 10 additions and 10 deletions

View File

@ -235,7 +235,7 @@ Now let's update our ``index`` view in ``polls/views.py`` to use the template:
:filename: polls/views.py :filename: polls/views.py
from django.http import HttpResponse from django.http import HttpResponse
from django.template import RequestContext, loader from django.template import loader
from .models import Question from .models import Question
@ -243,10 +243,10 @@ Now let's update our ``index`` view in ``polls/views.py`` to use the template:
def index(request): def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5] latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html') template = loader.get_template('polls/index.html')
context = RequestContext(request, { context = {
'latest_question_list': latest_question_list, 'latest_question_list': latest_question_list,
}) }
return HttpResponse(template.render(context)) return HttpResponse(template.render(context, request))
That code loads the template called ``polls/index.html`` and passes it a That code loads the template called ``polls/index.html`` and passes it a
context. The context is a dictionary mapping template variable names to Python context. The context is a dictionary mapping template variable names to Python
@ -278,9 +278,9 @@ rewritten:
return render(request, 'polls/index.html', context) return render(request, 'polls/index.html', context)
Note that once we've done this in all these views, we no longer need to import Note that once we've done this in all these views, we no longer need to import
:mod:`~django.template.loader`, :class:`~django.template.RequestContext` and :mod:`~django.template.loader` and :class:`~django.http.HttpResponse` (you'll
:class:`~django.http.HttpResponse` (you'll want to keep ``HttpResponse`` if you want to keep ``HttpResponse`` if you still have the stub methods for ``detail``,
still have the stub methods for ``detail``, ``results``, and ``vote``). ``results``, and ``vote``).
The :func:`~django.shortcuts.render` function takes the request object as its The :func:`~django.shortcuts.render` function takes the request object as its
first argument, a template name as its second argument and a dictionary as its first argument, a template name as its second argument and a dictionary as its

View File

@ -72,13 +72,13 @@ MIME type :mimetype:`application/xhtml+xml`::
This example is equivalent to:: This example is equivalent to::
from django.http import HttpResponse from django.http import HttpResponse
from django.template import RequestContext, loader from django.template import loader
def my_view(request): def my_view(request):
# View code here... # View code here...
t = loader.get_template('myapp/index.html') t = loader.get_template('myapp/index.html')
c = RequestContext(request, {'foo': 'bar'}) c = {'foo': 'bar'}
return HttpResponse(t.render(c), return HttpResponse(t.render(c, request),
content_type="application/xhtml+xml") content_type="application/xhtml+xml")
``render_to_response`` ``render_to_response``