Documentation patch for 404 handler (was: Re: [mod_python] Rendering a page as a 404?)

Sean Reifschneider jafo at
Sat Aug 28 10:23:11 EDT 2004

I'm attaching a patch to the documentation, which should adequately
document this behavior.  This patch is against CVS.

 "You took quite a chance..."  "You take a chance getting up in the morning,
 crossing the street, or sticking your face in a fan."  -- Police Squad
Sean Reifschneider, Member of Technical Staff <jafo at>, ltd. - Linux Consulting since 1995.  Qmail, Python, SysAdmin
-------------- next part --------------
--- modpython3.tex.old	2004-08-28 08:46:00.000000000 -0600
+++ modpython3.tex	2004-08-28 09:19:26.000000000 -0600
@@ -433,3 +433,33 @@
+\section{Your Own 404 Handler\label{tut-404-handler}}
+In some cases, you may wish to return a 404 (\constant{HTTP_NOT_FOUND}) or
+other non-200 result from your handler.  There is a trick here.  if you
+return \constant{HTTP_NOT_FOUND} from your handler, Apache will handle
+rendering an error page.  This can be problematic if you wish your handler
+to render it's own error page.
+In this case, you need to set \code{req.status = apache.HTTP_NOT_FOUND},
+render your page, and then \code{return(apache.OK)}:
+   from mod_python import apache
+   def handler(req):
+      if req.filename[-17:] == 'apache-error.html':
+         #  make Apache report an error and render the error page
+         return(apache.HTTP_NOT_FOUND)
+      if req.filename[-18:] == 'handler-error.html':
+         #  use our own error page
+         req.status = apache.HTTP_NOT_FOUND
+         pagebuffer = 'Page not here.  Page left, not know where gone.'
+      else:
+         #  use the contents of a file
+         pagebuffer = open(req.filename, 'r').read()
+      #  fall through from the latter two above
+      req.write(pagebuffer)
+      return(apache.OK)

More information about the Mod_python mailing list