Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Jinja2 with CherryPy 3.2
    primarykey
    data
    text
    <p>I have Python 3.2 set up with Apache via mod_wsgi. I have CherryPy 3.2 serving a simple "Hello World" web page. I'd like to start templating using Jinja2 as I build out the site. I'm new to Python and therefore don't know much about Python, CherryPy, or Jinja.</p> <p>Using the code below, I can load the site root (<code>/</code>) and the products page (<code>/products</code>) with their basic text. That at least lets me know I've got Python, mod_wsgi, and CherryPy set up somewhat properly.</p> <p>Because the site will have many pages, I'd like to implement the Jinja template in a way that prevents me from having to declare and render the template in each page handler class. As far as I can tell, the best way to do that is by wrapping the PageHandler, similar to these examples:</p> <ul> <li><a href="http://docs.cherrypy.org/dev/concepts/dispatching.html#replacing-page-handlers" rel="nofollow">http://docs.cherrypy.org/dev/concepts/dispatching.html#replacing-page-handlers</a></li> <li><a href="http://docs.cherrypy.org/stable/refman/_cptools.html#cherrypy._cptools.HandlerWrapperTool" rel="nofollow">http://docs.cherrypy.org/stable/refman/_cptools.html#cherrypy._cptools.HandlerWrapperTool</a></li> </ul> <p>I've implemented the code in the second example, but it doesn't change anything.</p> <p>[more details after code]</p> <p><strong>wsgi_handler.py</strong> - <em>A mash-up of a few tutorials and examples</em></p> <pre><code>import sys, os abspath = os.path.dirname(__file__) sys.path.append(abspath) sys.path.append(abspath + '/libs') sys.path.append(abspath + '/app') sys.stdout = sys.stderr import atexit import threading import cherrypy from cherrypy._cptools import HandlerWrapperTool from libs.jinja2 import Environment, PackageLoader # Import from custom module from core import Page, Products cherrypy.config.update({'environment': 'embedded'}) env = Environment(loader=PackageLoader('app', 'templates')) # This should wrap the PageHandler def interpolator(next_handler, *args, **kwargs): template = env.get_template('base.html') response_dict = next_handler(*args, **kwargs) return template.render(**response_dict) # Put the wrapper in place(?) cherrypy.tools.jinja = HandlerWrapperTool(interpolator) # Configure site routing root = Page() root.products = Products() # Load the application application = cherrypy.Application(root, '', abspath + '/app/config') </code></pre> <p><strong>/app/config</strong></p> <pre><code>[/] request.dispatch: cherrypy.dispatch.MethodDispatcher() </code></pre> <p><strong>core module classes</strong></p> <pre><code>class Page: exposed = True def GET(self): return "got Page" def POST(self, name, password): return "created" class Products: exposed = True def GET(self): return "got Products" def POST(self, name, password): return "created" </code></pre> <p>Based on what I read on a <a href="https://groups.google.com/group/cherrypy-users/browse_thread/thread/9c3d16ea3f781335?hide_quotes=no" rel="nofollow">Google Group</a> I figured I might need to "turn on" the Jinja tool, so I updated my config to this:</p> <p><strong>/app/config</strong></p> <pre><code>[/] tools.jinja.on = True request.dispatch: cherrypy.dispatch.MethodDispatcher() </code></pre> <p>After updating the config, the site root and products pages display an CherryPy generated error page "500 Internal Server Error". No detailed error messages are found in the logs (at least not in the logs I'm aware of).</p> <p>Unless it came pre-installed, I know I probably need the <a href="http://tools.cherrypy.org/wiki/Jinja" rel="nofollow">Jinja Tool</a> that's out there, but I don't know where to put it or how to enable it. How do I do that?</p> <p>Am I going about this the right way, or is there some better way?</p> <p><strong>Edit</strong> (21-May-2012):</p> <p>Here is the Jinja2 template I'm working with:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;{{ title }}&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Hello World&lt;/h1&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload