Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30906 closed Cleanup/optimization (fixed)

Error Outputting CSV code example. Template.render() does not accept Context objects.

Reported by: Philipp Maino Owned by: Hasan Ramezani
Component: Documentation Version: 2.2
Severity: Normal Keywords: Context, template, loader, render
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

https://docs.djangoproject.com/en/3.0/howto/outputting-csv/

We can find in the section "Using the template system" in the docs about "Outputing csv with django" the following code:

t = loader.get_template('my_template_name.txt')
c = Context({
      'data': csv_data,
})
response.write(t.render(c))

The django.template.loader.render is expecting a dict though and not a Context object as we can see here:

class Template:

    def __init__(self, template, backend):
        self.template = template
        self.backend = backend

    @property
    def origin(self):
        return self.template.origin

    def render(self, context=None, request=None):
        context = make_context(context, request, autoescape=self.backend.engine.autoescape)
     .
     .
     .


def make_context(context, request=None, **kwargs):
"""
Create a suitable Context from a plain dict and optionally an HttpRequest.
"""
if context is not None and not isinstance(context, dict):
    raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)

Change History (7)

comment:1 by Carlton Gibson, 5 years ago

Easy pickings: set
Triage Stage: UnreviewedAccepted

Yes, thank you. Good spot.

comment:2 by Carlton Gibson, 5 years ago

Summary: Error in documentation: django.template.loader.render function does not accept Context objectsError Outputting CSV code example. Template.render() does not accept Context objects.

comment:3 by Hasan Ramezani, 5 years ago

Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:4 by Hasan Ramezani, 5 years ago

Has patch: set
Last edited 5 years ago by Mariusz Felisiak (previous) (diff)

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 05c3ef26:

Fixed #30906 -- Fixed an example of using the template system to generate CSV.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 6df42e61:

[3.0.x] Fixed #30906 -- Fixed an example of using the template system to generate CSV.

Backport of 05c3ef26a203de1bc227e31b88999ff2e3b11abf from master

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 9d627bf9:

[2.2.x] Fixed #30906 -- Fixed an example of using the template system to generate CSV.

Backport of 05c3ef26a203de1bc227e31b88999ff2e3b11abf from master

Note: See TracTickets for help on using tickets.
Back to Top