Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:08:42+00:00 2026-06-04T05:08:42+00:00

I have Python 3.2 set up with Apache via mod_wsgi. I have CherryPy 3.2

  • 0

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.

Using the code below, I can load the site root (/) and the products page (/products) with their basic text. That at least lets me know I’ve got Python, mod_wsgi, and CherryPy set up somewhat properly.

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:

  • http://docs.cherrypy.org/dev/concepts/dispatching.html#replacing-page-handlers
  • http://docs.cherrypy.org/stable/refman/_cptools.html#cherrypy._cptools.HandlerWrapperTool

I’ve implemented the code in the second example, but it doesn’t change anything.

[more details after code]

wsgi_handler.py – A mash-up of a few tutorials and examples

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')

/app/config

[/]
request.dispatch: cherrypy.dispatch.MethodDispatcher()

core module classes

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"

Based on what I read on a Google Group I figured I might need to “turn on” the Jinja tool, so I updated my config to this:

/app/config

[/]
tools.jinja.on = True
request.dispatch: cherrypy.dispatch.MethodDispatcher()

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).

Unless it came pre-installed, I know I probably need the Jinja Tool that’s out there, but I don’t know where to put it or how to enable it. How do I do that?

Am I going about this the right way, or is there some better way?

Edit (21-May-2012):

Here is the Jinja2 template I’m working with:

<!DOCTYPE html>
<html>
    <head>
        <title>{{ title }}</title>
    </head>
    <body>
        <h1>Hello World</h1>
    </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:08:43+00:00Added an answer on June 4, 2026 at 5:08 am

    I figured it out.

    In the interpolator function, the next_handler function call the original PageHandler (Page.GET or Products.GET in this case). Those original PageHandlers return strings while the interpolator function is treating the response like a python dict (dictionary), hence the double asterisk when it’s passed to template.render as **response_dict.

    The Jinja template has a placeholder for title, so it needs a title to be defined. Passing a plain string to the render function doesn’t define what title should be. We need to pass an actual dict to the render function (or nothing at all, but what good is that?).

    Note: For either of these fixes, the jinja tool does need to be enabled, as shown in the question by setting tools.jinja.on to True in the config.

    Quick Fix

    Define the title as the render function is called. To do this I need to change this line:

    return template.render(**response_dict) # passing the string as dict - bad
    

    to this:

    return template.render(title=response_dict) # pass as string and assign to title
    

    Like this, the template renders with my PageHandler text as the page title.

    Better Fix

    Because the template will grow to be more complex, one render function probably won’t always be able to correctly assign the necessary placeholders. It’s probably a good idea to let the original page handler return an actual dict with the template’s many placeholders assigned.

    Leave the interpolator function as it was:

    def interpolator(next_handler, *args, **kwargs):
        template = env.get_template('base.html')
        response_dict = next_handler(*args, **kwargs)
        return template.render(**response_dict)
    

    Update Page and Products to return actual dicts that define the value’s for the template’s placeholders:

    class Page:
        exposed = True
    
        def GET(self):
            dict = {'title' : "got Page"}
            return dict
    
        def POST(self, name, password):
            # This should be updated too
            # I just haven't used it yet
            return "created"
    
    class Products:
        exposed = True
    
        def GET(self):
            dict = {'title' : "got Products"}
            return dict
    
        def POST(self, name, password):
            # This should be updated too
            # I just haven't used it yet
            return "created"
    

    Like this, the template renders with my PageHandler title text as the page title.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up Django on Windows 7 on Apache/mod_wsgi. I need to stop,
I have a small web.py Python application that I would like to serve under
[Django 1.0.2] I have a view set up like this: (r'^redirect/(?P<object_id>\d+)/(?P<url>.*)/$', 'django.views.generic.simple.redirect_to', {'content_type': SiteType},
I have a Django app running on Windows (via Apache + mod_wsgi) that's CPU-bound.
I have a python logger set up, using python's logging module. I want to
I have a Python regular expression that matches a set of filenames. How to
I have installed python 32 package to the C:\python32 I have also set the
I have managed to set up Cassandra + Thrift and the Python wrapper for
the set the i have so far is: windows 7 64bit Apache 2.2.14/mysql/php 5.3.1
I'm trying to set up Apache with mod_wsgi to run Django on a Red

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.