Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion project/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down
47 changes: 47 additions & 0 deletions project/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!-- templates/404.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Page not found</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 700px;
margin: 80px auto;
padding: 0 20px;
color: #222;
text-align: center;
}

h1 {
font-size: 48px;
margin-bottom: 10px;
}

p {
font-size: 18px;
margin-bottom: 30px;
}

a {
display: inline-block;
padding: 10px 16px;
background: #0d6efd;
color: white;
text-decoration: none;
border-radius: 4px;
}

a:hover {
background: #0b5ed7;
}
</style>
</head>
<body>
<h1>404</h1>
<p>The page you are looking for could not be found.</p>
<a href="/">Back to home</a>
</body>
</html>