[英]How to log redirects in Pyramid?
我正在嘗試對其進行設置,以使我的Pyramid應用程序HTTPFound
重定向時(例如,當我的某個視圖引發HTTPFound
時)記錄一條消息。
創建HTTPFound
的自定義子類是HTTPFound
但是必須確保該類在應用程序中的任何地方都使用很HTTPFound
。
然后,我想到了使用context=HTTPFound
創建自定義異常視圖的想法,但是該視圖似乎沒有被調用。
是否有一種標准方法可以為應用程序全局重定向設置特殊處理?
要注銷重定向,您將只有一個補間可以檢查返回狀態,例如:
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.httpexceptions import HTTPFound
def hello1(request):
raise HTTPFound(request.route_url('hello2'))
def hello2(request):
return {'boom': 'shaka'}
def redirect_logger(handler, registry):
def redirect_handler(request):
response = handler(request)
if response.status_int == 302:
print("OMGZ ITS NOT GOING WHERE YOU THINK")
return response
return redirect_handler
def main():
config = Configurator()
config.add_route('hello1', '/', view=hello1)
config.add_route('hello2', '/hi', view=hello2, renderer='json')
config.add_tween('__main__.redirect_logger')
app = config.make_wsgi_app()
return app
if __name__ == '__main__':
app = main()
server = make_server('0.0.0.0', 8080, app)
print("http://0.0.0.0:8080")
server.serve_forever()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.