Opened 9 years ago

Closed 8 years ago

#25854 closed Bug (fixed)

Remove deprecated usage of template.render() with RequestContext in docs

Reported by: stephanm Owned by: Alasdair Nicol
Component: Documentation Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by stephanm)

I am just switching from django 1.8.7 to 1.9 and stumbled on
a strange warning.

I have the similar code snippet in my view as explained in:
https://docs.djangoproject.com/en/1.9/intro/tutorial03/#write-views-that-actually-do-something

...
template = loader.get_template('polls/index.html')
context = RequestContext(request, { })
return HttpResponse(template.render(context))

Now I get the warning:

RemovedInDjango110Warning: render() must be called with a dict, not a RequestContext.

I am not sure how to work around this problem.

Is it forbidden to use RequestContext in template.render(..)
if yes, howto must I transform RequestContext in a dict.

In this case the documentation should be actualized.

By the way: can I download the tutorial poll application project somewhere?

Change History (17)

comment:1 by stephanm, 9 years ago

Description: modified (diff)

comment:2 by Aymeric Augustin, 9 years ago

Oops. Indeed, that part of the tutorial needs updating.

Actually this change was made in Django 1.8. It emitted a PendingDeprecationWarning which is silent by default. Since Django 1.9 it emits a DeprecationWarning which is loud.

As explained in the upgrade guide this code must be simplified to:

template = loader.get_template('polls/index.html')
return HttpResponse(template.render(context, request))

comment:3 by Aymeric Augustin, 9 years ago

Component: UncategorizedDocumentation
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug
Version: 1.91.8

comment:4 by Alasdair Nicol, 9 years ago

Owner: changed from nobody to Alasdair Nicol
Status: newassigned

I'll have a go at writing a patch for this.

As well as the code from the tutorial, I think there are other parts of the docs which could be updated. For example, the csrf docs say:

Use RequestContext, which always uses ...

I think we could say something like

Render your templates with the request object...

comment:5 by Aymeric Augustin, 9 years ago

Yes, that's a good change.

I likely missed a few spots in the docs when I made the related code changes; please fix any that you find!

comment:6 by Tim Graham, 9 years ago

Summary: Bad warning when using template.renderRemove deprecated usage of template.render() with RequestContext in docs

comment:7 by Alasdair Nicol, 9 years ago

Has patch: set

comment:8 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 32c7d93e:

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

comment:9 by Tim Graham <timograham@…>, 9 years ago

In 8eeb01a7:

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

Backport of 32c7d93e5f0638c5a1ec8e2525c967be444fcef1 from master

comment:10 by Tim Graham <timograham@…>, 9 years ago

In e95c9c3:

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

Backport of 32c7d93e5f0638c5a1ec8e2525c967be444fcef1 from master

comment:11 by Alasdair Nicol, 9 years ago

Resolution: fixed
Status: closednew

I just noticed one more example that might need to be updated, in the Subclassing RequestContext section.

def some_view(request):
    # ...
    c = RequestContext(request, {
        'foo': 'bar',
    }, [ip_address_processor])
    return HttpResponse(t.render(c))

This snippet doesn't show how t was created, but it won't work if it used loader.get_template. I'm not sure whether/how we should change this section.

In addition, I think that the Subclassing RequestContext could be improved slightly. I'm afraid it's just a thought and not a concrete patch at the moment, so it might be better to open a new ticket.

There are many places in the docs where we say 'use a request context' and link to this section, but at the moment, we don't actually say *how* to use a request context. As we say on the csrf docs, the user should do one of:

  1. Use a contrib app
  2. Use the render shortcut
  3. Use a generic CBV
  4. pass request to template.render(context, request)

comment:12 by Tim Graham, 9 years ago

I think we should rename that section to something like "Using RequestContext". The current title is a bit vague and unhelpful. Hopefully, when we say "use a RequestContext' -- RequestContext is linked and will direct readers toward this section. If we close this ticket and include fixing that example in the new ticket, that's fine with me.

comment:13 by Tim Graham, 9 years ago

Has patch: unset

comment:14 by Tim Graham, 8 years ago

Has patch: set

Can we close out this ticket with this patch?

comment:15 by Tim Graham <timograham@…>, 8 years ago

In f8c338ec:

Refs #25854 -- Completed a RequestContext docs example.

comment:16 by Tim Graham <timograham@…>, 8 years ago

In a033b351:

[1.10.x] Refs #25854 -- Completed a RequestContext docs example.

Backport of f8c338ec6a372841559e3134cf4d4329c460f8c7 from master

comment:17 by Tim Graham, 8 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top