kasavbere
kasavbere

Reputation: 6003

How to use jinja to generate xml file on GAE?

I know how to use jinja2 to generate html pages as response to urls on GAE ( https://developers.google.com/appengine/docs/python/gettingstartedpython27/templates).

I need to use jinja2 to create an actual xml file and save it on my server.

An example of someone using a tool called util is at http://blog.notdot.net/2009/10/Blogging-on-App-engine-part-9-Sitemaps-and-review or https://github.com/Arachnid/bloggart/blob/part9/static.py (on line 47).

A snippet of the UTIL example (never mind he is creating a sitemap):

rendered = utils.render_template('sitemap.xml', {'paths': paths})
set('/sitemap.xml', rendered, 'application/xml', False)

Can Jinja2 do something like that?

I have tried many things, including the following:

data = jinja_environment.get_template('for_xml.html')
fo = open("/myfile.xml","w+")
fo.write(data)
fo.close()

So far whether I use w+ or wb I get an error:

File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 632, in __init__
raise IOError('invalid mode: %s' % mode)
IOError: invalid mode: w+

When I use set as in the UTIL example, it says set needs 1 parameter not 4.

Upvotes: 2

Views: 976

Answers (3)

voscausa
voscausa

Reputation: 11706

When you need a dynamic sitemap, why not create it on the fly. Second: With Jinja2 you also can use XML templates = read XML templates and NOT ONLY html.

Conslusion: create a non static handler with serves the dynamic creation of the "sitemap.xml" and jinja will render your sitemap.xml result.

Upvotes: 0

Sebastian Kreft
Sebastian Kreft

Reputation: 8199

You cannot write a file to GAE server, you can however write a file to either blobstore or Cloud Storage. See the apis, as they provide some File-like object to write to.

ps: when using set you are getting an error, because it refers to the function that user defined and I guess in your code you are not including that definition, so you are in fact using the set constructor which takes only one parameter.

Upvotes: 1

Renan Ivo
Renan Ivo

Reputation: 1388

Google app engine sandbox will not allow you to write to the file system. -- Take a look at The Sandbox section here.

The sitemap example you cited shall be creating a XML response to the client.

Upvotes: 3

Related Questions