Opened 18 years ago

Closed 18 years ago

Last modified 17 years ago

#1169 closed enhancement (wontfix)

[patch] Render a template to a file on disk (loader.render_to_disk)

Reported by: eric@… Owned by: Adrian Holovaty
Component: Template system Version:
Severity: normal Keywords: loader, render_to_disk, render_to_string
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Here's a patch that allows one to render (output, save) a template to a file on disk. Use it in a similar way as you would use loader.render_to_string().

render_to_file loads the given template_name and renders it, with the given dictionary as context, to a file on disk. The template_name may be a string to load a single template using get_template, or it may be a tuple to use select_template to find one of the templates in the list. file_path defines the file that will be written to disk. mode defaults to 'w' which will create the file if it doesn't exist and will overwrite the file if it does exist.

Here's an example use:

from django.core.template import loader


path = '/path/to/my/file.txt'
template = 'django/path/to/template/name'
loader.render_to_file(template_name=template,
                      file_path=path,
                      dictionary={'foo': self.get_foo(),
                                  'bar': Bar(),})

Attachments (1)

django-core-template-loader.py.diff (1.2 KB ) - added by eric@… 18 years ago.
Patch to render a template to file on disk

Download all attachments as: .zip

Change History (3)

by eric@…, 18 years ago

Patch to render a template to file on disk

comment:1 by Adrian Holovaty, 18 years ago

What's the use case for this, and why is it necessary to include this functionality in Django proper, when it can be emulated in two lines of Python code using the normal render() method?

comment:2 by Adrian Holovaty, 18 years ago

Resolution: wontfix
Status: newclosed

Closing this ticket as a wontfix because my question wasn't answered.

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