(B站学习网址 https://www.bilibili.com/video/av93282315)
目录结构:
上代码:
post_test_5.py:
# -*- coding: UTF-8 -*-
from fastapi import FastAPI, Form
from starlette.requests import Request
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
app = FastAPI()
templates = Jinja2Templates(directory="templates")
app.mount('/static', StaticFiles(directory='static'), name='static')
@app.post("/user/")
async def files(
request: Request,
username: str = Form(...),
password: str = Form(...),
):
print('username', username)
print('password', password)
return templates.TemplateResponse(
'index.html',
{
'request': request,
'username': username,
}
)
@app.get("/")
async def main(request: Request):
return templates.TemplateResponse('signin.html', {
'request': request})
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="{
{ url_for('static', path='/imgs/favicon.ico') }}">
<title