jinja2 render with cherrypy


username dalaylama00.

My issue is that I have been trying to render some initial parameters to a html file, get data from the user and run it on the server side. Getting data from the client through json works just fine, but I cannot render the default parameters to the index.html. When I start the server, the browser shows literally the placeholders in the index.html file, as an example, instead of rendering 2000-01-31 , it shows {{start_time}}, which is the placeholder in the index.html file. By the way, this is working without any issues under my local server, by using native cherrypy server. Any help is appreciated:

Here is the portion that I think is not working:

enviro = Environment(loader=FileSystemLoader(env.path+os.sep+'html5up-strata'))
tmpl = enviro.get_template('index.html')
 defaults={'title':'BON', 'lege_data':'\"\"', 'start_time':"2000-01-31",'end_time':"2000-02-10",\
    'plot_header':'<p>generate plots.</p>',\

class Strata(object):

 def index(self, params={}):
 if (not params):
        return tmpl.render(defaults)
        return tmpl.render(params)

That sounds like it's not being processed by Jinja. Are you accessing the template as a static file instead of through your web app?

Hi Glen,

I am not quite sure how it is being handled, but if it helps, the template is a static file with many place holders in it , such as the one I provided, and all .css and .js files along with the template are under a folder called static.

Hi delaylama00,

Is Environment and .get_template() a cherrypy specific thing? do you have any documentation/references to it so we can read about it?

Hello conrad,

I apologize, I should have mentioned. Environment is part of jinja2, which is not rendering back on your server. Here is the description of the module:

By the way, you are welcome to have a look at the code. It is the _wsgi file under /var/www.

You're serving index.html from your static file directory in /var/www it's not going anywhere near any template or cherrypy code.

Hi Glenn,

What is you recommendation ? What steps should I take to fix this issue ? Thanks a lot.

In your "Static files" setup on the "Web" tab, you have a mapping from the blank URL to a specific directory. That's telling PythonAnywhere that any request made to your web app should bypass your code completely and just serve up the file that's located in that directory instead.

What were you trying to do with that static file setup?

Hello giles,

Yes thank you, that worked just fine. The problem was exactly as you said. Once I change the directory in the mapping it worked flawlessly.

Excellent, thanks for confirming!