Saturday, January 17, 2015

How to set TEMPLATE_PATH for Python Bottle framework in Windows Environment

By default template path is set to ./ or ./views/ in bottle.py, however this will not work and the following error message appear always in Windows environment.
Error: 500 Internal Server Error
Sorry, the requested URL 'http://localhost:8080/' caused an error:
Template 'hello_world' not found.


To overwrite a template path “template_lookup” should be used under return bottle.template function as shown below:
template_lookup=['C:/Python34/Lib/site-packages/views']
*Make sure that forward slash used instead of backslash.
The following sample code illustrates this:
***********************************************
Python Code
***********************************************
import bottle


@bottle.route('/')
def home_page():
   mythings = ['apple','orange','banana','peach']
   return bottle.template('hello_world', username="Uma", things=mythings
                          , template_lookup=['C:/Python34/Lib/site-packages/views'])


bottle.debug(True)
bottle.run(host='localhost', port=8080)

**********************************************
Template "hello_world.tpl"
**********************************************
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<p>
Welcome {{username}}
<p>
<ul>
%for thing in things:
<li>{{thing}}</li>
%end
</ul>
</body>
</html>



2 comments:

  1. Thanks for that, I was hitting my head against the wall to get it to work. Is template_looup an optional argument of the template() function? I cannot find it documented anywhere?

    ReplyDelete
  2. Thanks for sharing your innovative ideas to our vision. I have read your blog and I gathered some new information through your blog. Your blog is really very informative and unique. Keep posting like this. Awaiting for your further update.If you are looking for any Methods for Sets in Python related information, please visit our website Sets in Python Tutorial

    ReplyDelete