首页 > 其他分享 >[FastAPI-03]Form表单

[FastAPI-03]Form表单

时间:2022-11-11 21:13:42浏览次数:43  
标签:username 03 Form FastAPI request html post password

1. 安装依赖

pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com python-multipart

2. 表单程序

.
├── post_test_1.py
└── templates
    ├── index.html
    └── post.html

2.1 post_test_1.py

# _*_ coding: UTF-8 _*_
from fastapi import FastAPI,Form
from starlette.requests import Request
from starlette.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates(directory="../templates")

@app.post("/user/")
async def create_upload_files(request: Request,username:str =Form(),password:str = Form()):
    print('username',username)
    print('password',password)
    
    return templates.TemplateResponse('index.html',{'request':request,'username':username,'password':password})


@app.get("/")
async def main(request: Request):
    return templates.TemplateResponse('post.html',{'request':request})



if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app,host='127.0.0.1',port=8000)

2.2 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.0">
    <title>Document</title>
</head>
<body>
    <h1>Hello FastAPI</h1>
    <h2>{{ username }}</h2>
    <h2>{{ password }}</h2>
</body>
</html>

2.3 post.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= , initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="container">
        <form action="/user/" enctype="application/x-www-form-urlencoded" method="post">
            <label>username</label>
            <br>
            <input type="username" name="username">
            <br>
            <label>password</label>
            <br>
            <input type="password" name="password">
            <br><br>
            <input type="submit">
        </form>
    </div>
</body>
</html>

标签:username,03,Form,FastAPI,request,html,post,password
From: https://www.cnblogs.com/leoshi/p/16882003.html

相关文章

  • 【TS】1103- 30个小知识让你更清楚TypeScript
    TypeScript是Microsoft开发的JavaScript的开源超集,用于在不破坏现有程序的情况下添加附加功能。由于其独特的优势,例如,静态类型和许多速记符号,TypeScript现在被前端和......
  • [FastAPI-02]模板渲染
    1.插件库pipinstall-ihttp://pypi.douban.com/simple/--trusted-hostpypi.douban.comjinja2aiofiles2.模板渲染程序2.1Python程序#_*_coding:UTF-8_*_......
  • py-03
    一、用户输入和while循环1、input()函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便你使用 使用int()来获取数......
  • [FastAPI-01]HelloWorld
    1.环境搭建/root/.pyenv/versions/3.9.14/bin/python3.9-mpipinstall-ihttp://pypi.douban.com/simple/--trusted-hostpypi.douban.com--upgradepippipinstal......
  • post请求传formdata格式
    constformData=newFormData();fileList.value.forEach(file=>{formData.append('multipartFile',file);});formData.append(......
  • AIR32F103(五) FreeRTOSv202112核心库的集成和示例代码
    目录AIR32F103(一)合宙AIR32F103CBT6开发板上手报告AIR32F103(二)Linux环境和LibOpenCM3项目模板AIR32F103(三)Linux环境基于标准外设库的项目模板AIR32F103(四)2......
  • GB2312、GB18030、GBK、UNICODE、B…
    1, 常用字符集分类ASCII及其扩展字符集作用:表语英语及西欧语言。位数:ASCII是用7位表示的,能表示128个字符;其扩展使用8位表示,表示256个字符。范围:ASCII从00到7F,扩展从00到FF。......
  • C语言 函数03 函数的调用
    传值调用函数的形参和实参分别占有不同内存块,对形参的修改不会影响实参。传址调用传址调用是把函数外部创建变量的内存地址传递给函数参数的一种调用函数的方式。这种传参方......
  • 关于定时程序,web/服务/winform
    https://wenda.so.com/q/1371555124068874?src=140&q=c%23%E5%AE%9A%E6%97%B6%E7%A8%8B%E5%BA%8Fhttps://blog.csdn.net/yudehui/article/details/9454947如果是web程序,......
  • python报错 'int' object has no attribute 'randint'
    先看我的代码importrandom##随机生成[1,10)步长为2random=random.randrange(1,10,2)print(random)r=random.randint(0,10)print(r)print(random.randint(......