diff --git a/project/main.py b/project/main.py index 52a7a4a..cdab601 100644 --- a/project/main.py +++ b/project/main.py @@ -1,5 +1,6 @@ from celery.result import AsyncResult -from fastapi import Body, FastAPI, Form, Request +from fastapi import Body, FastAPI, Request, HTTPException +from fastapi.exception_handlers import http_exception_handler from fastapi.responses import JSONResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates @@ -11,7 +12,19 @@ app.mount("/static", StaticFiles(directory="static"), name="static") templates = Jinja2Templates(directory="templates") +@app.exception_handler(404) +async def custom_404_handler(request: Request, exc: HTTPException): + accept_header = request.headers.get("accept", "") + request_url = request.url.path + if "text/html" in accept_header and not request_url.startswith("/tasks"): + return templates.TemplateResponse( + "404.html", + {"request": request}, + status_code=404, + ) + + return await http_exception_handler(request, exc) @app.get("/") def home(request: Request): diff --git a/project/templates/404.html b/project/templates/404.html new file mode 100644 index 0000000..6d9e6bf --- /dev/null +++ b/project/templates/404.html @@ -0,0 +1,47 @@ + + + +
+ + +The page you are looking for could not be found.
+ Back to home + + \ No newline at end of file