Render to file

1
2
3
4
from django.template.loader import render_to_string

def render_to_file(template, filename, context):
    open(filename, "w").write(render_to_string(template, context))

More like this

  1. Caching XHTML render_to_response by smoonen 4 years, 10 months ago
  2. Partial OpenID provider implementation from idproxy.net by simon 5 years, 10 months ago
  3. table with n items per row using custom modulo tag by elgreengeeto 4 years, 5 months ago
  4. a simple tag with context by dsblank 3 years, 5 months ago
  5. Switch/case tags. by jacobian 5 years, 10 months ago

Comments

jezdez (on April 9, 2008):

indeed, very useful!

#

tekNico (on April 12, 2008):

Yeah, useful while living in fantASCII land: then you realize you need codecs.open instead. ;-)

#

davmuz (on July 31, 2012):

The Django template engine renders as UTF-8, so you have to write a UTF-8 file.

``import codecs

codecs.open(filename, 'w', 'utf-8').write(render_to_string(template, context))``

#

(Forgotten your password?)