from pyramid.view import notfound_view_config from pyramid.httpexceptions import HTTPNotFound @notfound_view_config(renderer='templates/404.jinja2') def notfound_view(request): raise HTTPNotFound('Page not found')
from pyramid.view import view_config from pyramid.httpexceptions import HTTPNotFound from pyramid.response import Response import json @view_config(context=HTTPNotFound) def notfound_view(request): response = Response( json.dumps({'error': 'Page not found'}), status='404 Not Found', content_type='application/json' ) return responseIn this example, we define a view function decorated with the `view_config` decorator that will be called when an HTTPNotFound exception is raised. We create a JSON response with a custom error message and return it with a 404 status code. In both examples, HTTPNotFound is used to handle 404 errors in a Pyramid application. It is part of the pyramid.httpexceptions package library.