【发布时间】:2021-05-14 06:24:06
【问题描述】:
我正在开发一个简单的 meme 发布网站项目,我从用户那里获取UserName、caption、Image URL,并希望在网站主页上显示所有 meme。
我是一名全栈开发人员,我使用 FastAPI 作为后端并浏览了它的文档。到目前为止,我已经能够通过 Post 请求从 HTML 向服务器获取响应,但现在我想将数据存储在某个数据库中并将该数据发送回网站的主页。我尝试查找教程视频,但无法获得可以帮助我的视频。 任何形式的帮助表示赞赏。
下面是一些关于我如何执行当前请求的代码。
from fastapi import Request, status, BackgroundTasks
from fastapi.responses import JSONResponse
from tutorial import app,templates
import time
from datetime import datetime
from pydantic import BaseModel
from typing import Optional
class Record(BaseModel):
name:str
caption:str
meme_type: Optional[str] = None
img_meme:str
@app.post("/")
async def create_record(record: Record):
background_tasks.add_task(_run_task,record)
return record
def _run_task(name: str,caption: str,meme_type:str,img_meme: str):
time.sleep(3)
with open("memes.txt",mode="a") as file:
now=datetime.now()
content=f""" "Name":{name} \nCaption:{caption} \nURL:{img_meme} : now}\n"""
file.write(content)
【问题讨论】:
标签: html database backend fastapi