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 )
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 , 9 years ago
Description: | modified (diff) |
---|
comment:2 by , 9 years ago
comment:3 by , 9 years ago
Component: | Uncategorized → Documentation |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
Version: | 1.9 → 1.8 |
comment:4 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
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 , 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 , 9 years ago
Summary: | Bad warning when using template.render → Remove deprecated usage of template.render() with RequestContext in docs |
---|
comment:11 by , 9 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
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:
- Use a contrib app
- Use the render shortcut
- Use a generic CBV
- pass request to template.render(context, request)
comment:12 by , 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 , 9 years ago
Has patch: | unset |
---|
comment:17 by , 8 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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 aDeprecationWarning
which is loud.As explained in the upgrade guide this code must be simplified to: