如果存在 HTTPException,我如何使用 FastAPI 重定向请求?
在 Flask 中,我们可以这样实现:
@app.errorhandler(404)
def handle_404(e):
if request.path.startswith('/api'):
return render_template('my_api_404.html'), 404
else:
return redirect(url_for('index'))
或者在 Django 中我们可以使用 django.shortcuts:
from django.shortcuts import redirect
def view_404(request, exception=None):
return redirect('/')
我们如何使用 FastAPI 实现这一目标?
江户川乱折腾
HUX布斯
FFIVE
子衿沉夜
随时随地看视频慕课网APP
相关分类