[mod_python] little problem with my handler

Julien Cigar jcigar at ulb.ac.be
Tue Mar 28 05:36:46 EST 2006


I'm writing my first home-made handler (inspired by mod_publisher).
I wanted to do a "ruby on rails"-like dispatcher, so when I call : 
http://somehost.com/foo/bar it should call the bar() method of the class 
(controller) foo which lives under controllers/
Please note that it is not at all complete, I'm just playing with it at 
the moment ..

I face a strange problem, when I render my html file with my template 
engine (clearsilver), the problem comes from the line:
<link rel="stylesheet" type="text/css" href="styles/screen.css"></link>

Here is the error :

[notice] mod_python: (Re)importing module 'handler'
[notice] mod_python: (Re)importing module 'controllers.default'
[error] PythonHandler handler: Traceback (most recent call last):, 
referer: http://canis/ias/default/
[error] PythonHandler handler:   File 
"/usr/lib/python2.3/site-packages/mod_python/apache.py", line 299, in 
HandlerDispatch\n    result = object(req), referer: 
[error] PythonHandler handler:   File 
"/home/jcigar/public_html/invasive_species/public/handler.py", line 35, 
in handler\n    object = getattr(module, method), referer: 
[error] PythonHandler handler: AttributeError: 'module' object has no 
attribute 'styles', referer: http://canis/ias/default/

Here is the code of my handler:


import sys

from mod_python import apache, util


def handler(req):

    req.allow_methods(["GET", "POST"])
    if req.method not in (["GET", "POST"]):
        raise apache.SERVER_RETURN, apache.HTTP_METHOD_NOT_ALLOWED
    (controller, method) = ("default", "index")

    if req.path_info:
        path_info = req.path_info.strip('/').split('/')

    if path_info[0].isalpha():
        controller = path_info[0]
        if len(path_info) > 1 and path_info[1].isalpha():
            method = path_info[1]
    config = req.get_config()
    (autoreload, log) = (int(config.get("PythonAutoReload", 1)), 
int(config.get("PythonDebug", 0)))
        module = apache.import_module('controllers.%s' % (controller, ), 
autoreload=autoreload, log=log)
    except ImportError:
        req.log_error('Cannot import controller %s' % (controller, ))

    object = getattr(module, method)

    if callable(object):
        req.params = util.FieldStorage(req, keep_blank_values=1)
        result = util.apply_fs_data(object, req.params, req=req)
        req.log_error('%s is not callable' % (object, ))
        return apache.HTTP_INTERNAL_SERVER_ERROR

    if result or req.bytes_sent or req.next:
        req.content_type = 'text/html'
        return apache.OK
        req.log_error('Nothing returned')
        return apache.HTTP_INTERNAL_SERVER_ERROR

In advance, thanks for support !

Julien Cigar
Belgian Biodiversity Platform
Université Libre de Bruxelles
Campus de la Plaine CP 257
Bâtiment NO, Bureau 4 N4 115C (Niveau 4)
Boulevard du Triomphe, entrée ULB 2
B-1050 Bruxelles
Work: jcigar at ulb.ac.be
Personal: mage at mordor.ath.cx

More information about the Mod_python mailing list