You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mod_python-commits@quetz.apache.org by gr...@apache.org on 2004/08/30 16:58:47 UTC

cvs commit: httpd-python/Doc modpython3.tex

grisha      2004/08/30 07:58:47

  Modified:    Doc      modpython3.tex
  Log:
  Documentation patch for 404 handler
  Submitted by:	Sean Reifschneider
  
  Revision  Changes    Path
  1.19      +30 -0     httpd-python/Doc/modpython3.tex
  
  Index: modpython3.tex
  ===================================================================
  RCS file: /home/cvs/httpd-python/Doc/modpython3.tex,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- modpython3.tex	30 Sep 2003 21:17:54 -0000	1.18
  +++ modpython3.tex	30 Aug 2004 14:58:47 -0000	1.19
  @@ -433,3 +433,33 @@
   
   \end{itemize}
   
  +\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)}:
  +
  +\begin{verbatim}
  +   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)
  +\end{verbatim}