Thanks. The technical aspects go a bit above my head but this is a good post for me to bookmark and use as a jumping off point for some reading.
After switching out .first() but before adding the 404 redirect I was getting a '<type 'exceptions.AttributeError'> 'NoneType' object has no attribute 'title'' error. The error ticket wasn't overly helpful on this one. Is this due to other issues with my terrible noob code? On Saturday, March 3, 2012 9:05:48 PM UTC-8, Anthony wrote: > > def view(): >> postid = request.args(0) >> post = db(db.blogPost.id == postid).select()[0] >> > > When the postid doesn't exist, the above select will return an empty Rows > object, so subscripting it with [0] results in the IndexError. The web2py > framework has no way of knowing that this particular Python exception > represents a missing resource and should thus return a 404 response. If a > URL requests an application, controller, function, or static file that > doesn't exist, the framework knows to issue a 404, but general application > code exceptions are treated as 500 server errors by default. If you want, > you can raise your own 404: > > post = db(db.blogPost.id == postid).select().first() > if not post: > raise HTTP(404, 'sorry, that post does not exist') > > Note, it is better to use .first() to select the first row of the result > because if there are no rows, it will simply return None instead of raising > an exception. If you want a nicer 404 page, you can either use > routes_on_error to catch the 404 and point to either a static file or an > error handling action, or you can directly redirect to an error handling > action instead of raising the HTTP(404). If you create an error handling > action, make sure it sets response.status = 404 before returning so the > browser receives the correct response code with the page. > > Anthony > On Saturday, March 3, 2012 9:05:48 PM UTC-8, Anthony wrote: > > def view(): >> postid = request.args(0) >> post = db(db.blogPost.id == postid).select()[0] >> > > When the postid doesn't exist, the above select will return an empty Rows > object, so subscripting it with [0] results in the IndexError. The web2py > framework has no way of knowing that this particular Python exception > represents a missing resource and should thus return a 404 response. If a > URL requests an application, controller, function, or static file that > doesn't exist, the framework knows to issue a 404, but general application > code exceptions are treated as 500 server errors by default. If you want, > you can raise your own 404: > > post = db(db.blogPost.id == postid).select().first() > if not post: > raise HTTP(404, 'sorry, that post does not exist') > > Note, it is better to use .first() to select the first row of the result > because if there are no rows, it will simply return None instead of raising > an exception. If you want a nicer 404 page, you can either use > routes_on_error to catch the 404 and point to either a static file or an > error handling action, or you can directly redirect to an error handling > action instead of raising the HTTP(404). If you create an error handling > action, make sure it sets response.status = 404 before returning so the > browser receives the correct response code with the page. > > Anthony >

