Hi I'm trying to link to an json file from html code using fastapi and jinja2 but the link is not working. I guess i have to tell fastapi how to find the file or something like that.. I get an json (api) answer instad of the file..
{"detail":"Not Found"}
The python code:
from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
app = FastAPI()
templates = Jinja2Templates(directory='templates')
@app.get('/data', response_class=HTMLResponse)
async def data(request: Request):
return templates.TemplateResponse('data.html', {
'request': request
})
And the HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js"></script>
<title>Data</title>
</head>
<body>
<div class="ui container">
<h1>
Data downloader:
</h1>
<a href="../json/data.json" target="_blank">
Data
</a>
</div>
</body>
</html>
hrefis wrong. If it's a path to a route, you don't need the.., the same if thedata.jsonfile is in the same folder