第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

FastAPI處理和重定向404

FastAPI處理和重定向404

江戶川亂折騰 2023-03-16 16:39:33
如果存在 HTTPException,我如何使用 FastAPI 重定向請求?在 Flask 中,我們可以這樣實現(xiàn):@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 redirectdef view_404(request, exception=None):    return redirect('/')我們如何使用 FastAPI 實現(xiàn)這一目標?
查看完整描述

3 回答

?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

我們可以通過使用 FastAPI 的exception_handler來實現(xiàn):


如果你趕時間,你可以使用這個:


from fastapi.responses import RedirectResponse

from starlette.exceptions import HTTPException as StarletteHTTPException


@app.exception_handler(StarletteHTTPException)

async def custom_http_exception_handler(request, exc):

    return RedirectResponse("/")

但更具體的方法是,您可以創(chuàng)建自己的異常處理程序:


class UberSuperHandler(StarletteHTTPException):

    pass

    

def function_for_uber_super_handler(request, exc):

    return RedirectResponse("/")



app.add_exception_handler(UberSuperHandler, function_for_uber_super_handler)


查看完整回答
反對 回復 2023-03-16
?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

我知道為時已晚,但這是以您個人的方式處理 404 異常的最短方法。


重定向


from fastapi.responses import RedirectResponse



@app.exception_handler(404)

async def custom_404_handler(_, __):

    return RedirectResponse("/")

自定義神社模板


from fastapi.templating import Jinja2Templates

from fastapi.staticfiles import StaticFiles


templates = Jinja2Templates(directory="templates")

app.mount("/static", StaticFiles(directory="static"), name="static")


@app.exception_handler(404)

async def custom_404_handler(request, __):

    return templates.TemplateResponse("404.html", {"request": request})

從文件提供 HTML


@app.exception_handler(404)

async def custom_404_handler(_, __):

    return FileResponse('./path/to/404.html')

直接提供 HTML


from fastapi.responses import HTMLResponse


response_404 = """

<!DOCTYPE html>

<html>

<head>

    <title>Not Found</title>

</head>

<body>

    <p>The file you requested was not found.</p>

</body>

</html>

"""

    

@app.exception_handler(404)

async def custom_404_handler(_, __):

    return HTMLResponse(response_404)

注意:exception_handler裝飾器將當前request和exception作為參數傳遞給函數。我用過_并且__不需要變量。


查看完整回答
反對 回復 2023-03-16
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

我用這個方法,


from fastapi.responses import RedirectResponse

from starlette.exceptions import HTTPException as StarletteHTTPException


app.mount("/static", StaticFiles(directory="static"), name="static")


templates = Jinja2Templates(directory="templates")


@app.exception_handler(StarletteHTTPException)

async def custom_http_exception_handler(request, exc):

    return templates.TemplateResponse("404.html", {"request": request})

確保你有靜態(tài)文件夾,用于靜態(tài)文件和模板文件夾,用于 html 文件。


查看完整回答
反對 回復 2023-03-16
  • 3 回答
  • 0 關注
  • 701 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號