来自 html 的 Link 个文件,使用 fastapi 和 jinja2
Link files from html using fastapi and jinja2
您好,我正在尝试使用 fastapi 和 jinja2 从 html 代码 link 到 json 文件,但是 link 不工作.我想我必须告诉 fastapi 如何找到文件或类似的东西.. 我得到了文件的 json (api) 答案..
{"detail":"Not Found"}
python代码:
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
})
和 HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/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>
这段代码做到了..
python:
from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/json", StaticFiles(directory="json"), name="json")
templates = Jinja2Templates(directory='templates')
@app.get('/data', response_class=HTMLResponse)
async def data(request: Request):
return templates.TemplateResponse('data.html', {
'request': request
})
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
<title>Data</title>
</head>
<body>
<div class="ui container">
<h1>
Data downloader:
</h1>
<a href="{{ url_for('json', path='/data.json') }}" target="_blank">
Data
</a>
</div>
</body>
</html>
所以第一个代码 dint
是 StaticFiles
的导入和使用
您好,我正在尝试使用 fastapi 和 jinja2 从 html 代码 link 到 json 文件,但是 link 不工作.我想我必须告诉 fastapi 如何找到文件或类似的东西.. 我得到了文件的 json (api) 答案..
{"detail":"Not Found"}
python代码:
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
})
和 HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/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>
这段代码做到了..
python:
from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/json", StaticFiles(directory="json"), name="json")
templates = Jinja2Templates(directory='templates')
@app.get('/data', response_class=HTMLResponse)
async def data(request: Request):
return templates.TemplateResponse('data.html', {
'request': request
})
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
<title>Data</title>
</head>
<body>
<div class="ui container">
<h1>
Data downloader:
</h1>
<a href="{{ url_for('json', path='/data.json') }}" target="_blank">
Data
</a>
</div>
</body>
</html>
所以第一个代码 dint
是StaticFiles
的导入和使用