赞
踩
目前群友已超过3000人,添加时最好的备注方式为:来源+兴趣方向,技术交流、核心代码分享都搞定。
方式①、添加微信号:dkl88191,备注:来自CSDN+FastApi
方式②、微信搜索公众号:Python学习与数据挖掘,后台回复:FastApi
它由 Sebastian Ramirez 开发
Python、Pycharm
https://www.runoob.com/python/python-install.html
runoob.com/w3cnote/pycharm-windows-install.html
main.py
import uvicorn
from fastapi import FastAPI
app=FastAPI()
if __name__ == '__main__':
uvicorn.run(app)
服务器运行
添加接口
main
import uvicorn
from fastapi import FastAPI
app=FastAPI()
# 添加首页
@app.get("/")
def index():
return "This is Home Page."
if __name__ == '__main__':
uvicorn.run(app)
重新运行项目
重新项目
json数据格式解析
https://www.json.cn/
文档解析
文档备注信息
POST请求
使用ApiPost接口测试工具来访问接口
定义多种请求格式
main.py
import uvicorn
from fastapi import FastAPI
app=FastAPI()
@app.get("/user/{id}")
def user(id):
return {"id":id}
if __name__ == '__main__':
uvicorn.run(app)
main.py
import uvicorn
from fastapi import FastAPI,Header
app=FastAPI()
@app.get("/user")
def user(id, token=Header(None)):
return {"id":id,"token":token}
if __name__ == '__main__':
uvicorn.run(app)
安装包
python-multipart
main.py
import uvicorn
from fastapi import FastAPI,Header,Form
app=FastAPI()
@app.get("/user")
def user(id, token=Header(None)):
return {"id":id,"token":token}
@app.post("/login")
def login(username=Form(None),password=Form(None)):
return {"data":{"username":username,"password":password}}
if __name__ == '__main__':
uvicorn.run(app)
main.py
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse
app=FastAPI()
@app.get("/user")
def user():
return JSONResponse(content={"msg":"get user"},
status_code=202,
headers={"a":"b"})
if __name__ == '__main__':
uvicorn.run(app)
main.py
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse,HTMLResponse
app=FastAPI()
@app.get("/user")
def user():
return JSONResponse(content={"msg":"get user"},
status_code=202,
headers={"a":"b"})
@app.get("/")
def user():
html_content="""
<html>
<body><p style="color:red">Hello World</p></body>
</html>
"""
return HTMLResponse(content=html_content)
if __name__ == '__main__':
uvicorn.run(app)
main.py
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse,HTMLResponse
from starlette.responses import FileResponse
app=FastAPI()
@app.get("/user")
def user():
return JSONResponse(content={"msg":"get user"},
status_code=202,
headers={"a":"b"})
@app.get("/")
def user():
html_content="""
<html>
<body><p style="color:red">Hello World</p></body>
</html>
"""
return HTMLResponse(content=html_content)
@app.get("/avatar")
def user():
avatar='./static/violet.jpg'
return FileResponse(avatar)
if __name__ == '__main__':
uvicorn.run(app)
main.py
import uvicorn
from fastapi import FastAPI,Request
from fastapi.templating import Jinja2Templates
app=FastAPI()
template=Jinja2Templates("pages")
@app.get("/")
def user(req:Request):
return template.TemplateResponse("index.html",context={"request":req})
if __name__ == '__main__':
uvicorn.run(app)
返回结果
main.py
index.html
运行项目
main.py
index.html
运行项目
安装 tortoise-orm
安装 aiomysql
main.py
models.py
from tortoise import Model,fields
class Todo(Model):
id=fields.IntField(pk=True)
content=fields.CharField(max_length=500)
create_at=fields.DatetimeField(auto_now_add=True)
updated_at=fields.DatetimeField(auto_now=True)
main.py
import uvicorn
from fastapi import FastAPI, Request, Form
from fastapi.responses import RedirectResponse
from starlette.templating import Jinja2Templates
from tortoise.contrib.fastapi import register_tortoise
from dao.models import Todo
app=FastAPI()
template=Jinja2Templates("pages")
# 数据库绑定
register_tortoise(app,db_url="mysql://root:123456@localhost:3306/fastapi",
modules={"models":['dao.models']},
add_exception_handlers=True,
generate_schemas=True)
todos = ["写日记", "看电影", "玩游戏"]
# 添加首页
@app.get("/")
async def user(req:Request):
todos=await Todo.all()
print(todos)
return template.TemplateResponse("index.html",context={"request":req,"todos":todos})
@app.post("/todo")
def todo(todo=Form(None)):
todos.insert(0,todo)
return RedirectResponse("/",status_code=302)
if __name__ == '__main__':
uvicorn.run(app)
运行项目
main.py
运行项目
数据库里就多了个字段
此时就是显示有问题
index.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。