templating a non-html file (in view)

18 views
Skip to first unread message

Tim Michelsen

unread,
Apr 22, 2009, 6:37:38 PM4/22/09
to web...@googlegroups.com
Hello,
I would like to use the following how-to to generate a kml file from my
data base selection:

Better Python Practices for the GeoWeb
http://sgillies.net/blog/584/better-python-practices-for-the-geoweb/

<?xml version="1.0" encoding="utf-8"?>
<kml
xmlns="http://earth.google.com/kml/2.1"
xmlns:py="http://genshi.edgewall.org/"
>
<Folder>
<Style id="fireIcon">
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pal3/icon38.png</href>
</Icon>
</IconStyle>
</Style>
<Placemark py:for="item in collection">
<name py:content="item['name']">NAME</name>
<styleUrl>#fireIcon</styleUrl>
<description py:content="item['description']">
DESCRIPTION
</description>
<Point>
<coordinates py:content="item['coordinates']">
LONG,LAT
</coordinates>
</Point>
</Placemark>
</Folder>
</kml>

I created a function that works in other files called "kml".

Now created a file kml.kml in views/default:

<?xml version="1.0" encoding="utf-8"?>
<kml
xmlns="http://earth.google.com/kml/2.1"
>
<Folder>
<Style id="fireIcon">
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pal3/icon38.png</href>
</Icon>
</IconStyle>
</Style>
{{for placemark in placemarks:}}
<Placemark>
<name >{{=placemark.name}}</name>
<styleUrl>#fireIcon</styleUrl>
<description">
{{=placemark.description}}
</description>
<Point>
<coordinates >
{{=placemark.lon}}, {{=placemark.lat}}
</coordinates>
</Point>
</Placemark>
{{pass}}
</Folder>
</kml>


But when I enter the URL in the browser I get an invalid request.
Why?
If I rename the file to kml.html it returns the query result only.

How do I need to change my controller that web2py delivers xml using my
kml.kml template file?

Thanks in advance,
Timmie

mdipierro

unread,
Apr 22, 2009, 7:54:38 PM4/22/09
to web2py Web Framework
If you app is called "yourapp" and you put this file in

applications/yourapp/views/default/kml.html

you also need to edit

applications/yourapp/controllers/default.py

and add an action

def kml():
placemarks=db(...).select(....) ## fill the ...
return dict(placemarks=placemarks)

then you visit

http://127.0.0.1:8000/yourapp/default/kml

this calls the action, the action calls the view file.

Hope this helps. I am not sure I understood the question.

Massimo

On Apr 22, 5:37 pm, Tim Michelsen <timmichel...@gmx-topmail.de> wrote:
> Hello,
> I would like to use the following how-to to generate a kml file from my
> data base selection:
>
> Better Python Practices for the GeoWebhttp://sgillies.net/blog/584/better-python-practices-for-the-geoweb/

Timmie

unread,
Apr 23, 2009, 4:28:05 AM4/23/09
to web2py Web Framework
> applications/yourapp/controllers/default.py
>
> and add an action
>
> def kml():
>      placemarks=db(...).select(....) ## fill the ...
>      return dict(placemarks=placemarks)
The action is already in there.
My question was aiming at how to indicate web2py to render the page as
xml (kml as subset up xml) and not as html.

If I name the file
applications/yourapp/views/default/kml.html
it shows the placemark variables.

But with the filename
applications/yourapp/views/default/kml.kml
it returns "invalid function".

I hope that my explanation is a bit clearer.

Do I need to add

response.headers['Content-Type']='text/x-kml'
response.headers['Content-Disposition']='attachment;

to my function in default.py ?

Thanks in advance,
Timmie

desfrenes

unread,
Apr 23, 2009, 6:09:21 AM4/23/09
to web2py Web Framework
You probably should not change the file extension, changing the
headers is sufficient to have your file served as xml

Timmie

unread,
Apr 23, 2009, 9:34:19 AM4/23/09
to web2py Web Framework
> You probably should not change the file extension, changing the
> headers is sufficient to have your file served as xml
Yes, I found it out.

added

response.headers['Content-Type']= 'application/vnd.google-earth.kml
+xml'

To the controller function.

Works like a charm now. I haven't even thought that creating KMl from
a database is that simple.

Everyone wanting to create KML with web2py and having difficulties:
you can drop be a note.

desfrenes

unread,
Apr 23, 2009, 2:37:19 PM4/23/09
to web2py Web Framework
Because web2py templates can be used for any text format, they
probably shouldn't have .html extension, but it allows to edit
templates in any regular html wysiwyg editors.

mdipierro

unread,
Apr 23, 2009, 2:51:04 PM4/23/09
to web2py Web Framework
yes that was the point.

You can give them a different extension and then say in the controller
action

response.view='%s/%s.kml' % (request.controller, request.action)

but if they are not .html they may not be editable via admin.

Massimo
Reply all
Reply to author
Forward
0 new messages