当前位置: 首页 > news >正文

香港的网站不需要备案吗微信营销成功案例8个

香港的网站不需要备案吗,微信营销成功案例8个,绍兴市政府门户网站,怎么用id导入wordpress在现代Web开发中,FastAPI以其高性能和简洁的代码结构成为了构建RESTful API的热门选择。而Axios则因其基于Promise的HTTP客户端特性,成为了前端与后端交互的理想工具。本文将介绍FastAPI和Axios的结合使用,通过一个用户增删改查(C…

在现代Web开发中,FastAPI以其高性能和简洁的代码结构成为了构建RESTful API的热门选择。而Axios则因其基于Promise的HTTP客户端特性,成为了前端与后端交互的理想工具。本文将介绍FastAPI和Axios的结合使用,通过一个用户增删改查(CRUD)接口的实例,展示如何构建和请求API。

FastAPI简介

FastAPI是一个现代、快速的Web框架,用于构建API。它基于Python 3.6+类型提示,结合了Pydantic和Starlette的功能,提供了数据验证和序列化。FastAPI的核心优势在于其高性能、易用性、自动化文档生成以及对现代Python编程实践的深度支持。

Axios简介

Axios是一个基于Promise的HTTP客户端,广泛用于浏览器和Node.js环境。它以其简洁的API和强大的功能,成为了现代Web开发中不可或缺的工具。Axios的核心特性包括从浏览器创建XMLHttpRequests、从Node.js创建http请求、支持Promise API、拦截请求和响应、转换请求和响应数据、取消请求等。

FastAPI开发用户CRUD接口案例

以下是一个使用FastAPI和SQLModel实现用户CRUD操作的简单案例:

1. 安装依赖

首先,确保安装了FastAPI和SQLModel:

pip install fastapi uvicorn sqlmodel

2. 定义模型

models.py中定义用户模型:

from sqlmodel import SQLModel, Field

class UserBase(SQLModel):
    name: str
    age: int = Field(default=None, nullable=True)

class User(UserBase, table=True):
    id: int = Field(default=None, primary_key=True)

3. 创建数据库引擎

database.py中设置数据库连接和表:

from sqlmodel import create_engine

DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(DATABASE_URL)

4. 实现CRUD操作

crud.py中实现CRUD操作:

from models import User
from database import engine
from sqlmodel import Session, select

def get_user(user_id: int):
    stmt = select(User).where(User.id == user_id)
    with Session(engine) as session:
        return session.exec(stmt).first()

def create_user(user: User):
    with Session(engine) as session:
        session.add(user)
        session.commit()
        session.refresh(user)
        return user

def update_user(user_id: int, user: User):
    user.id = user_id
    with Session(engine) as session:
        session.add(user)
        session.commit()
        session.refresh(user)
        return user

def delete_user(user_id: int):
    user = get_user(user_id)
    if user:
        with Session(engine) as session:
            session.delete(user)
            session.commit()
            return True
    return False

5. 创建API端点

main.py中创建API端点:

from fastapi import FastAPI, HTTPException, Depends
from database import engine
from crud import create_user, get_user, update_user, delete_user
from models import User

app = FastAPI()

@app.on_event("startup")
def on_startup():
    SQLModel.metadata.create_all(engine)

@app.post("/users/", response_model=User)
def create_user_endpoint(user: User):
    return create_user(user)

@app.get("/users/{user_id}", response_model=User)
def read_user(user_id: int):
    user = get_user(user_id)
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    return user

@app.put("/users/{user_id}", response_model=User)
def update_user_endpoint(user_id: int, user: User):
    user.id = user_id
    updated_user = update_user(user_id, user)
    if not updated_user:
        raise HTTPException(status_code=404, detail="User not found")
    return updated_user

@app.delete("/users/{user_id}", response_class=HTTPException)
def delete_user_endpoint(user_id: int):
    if not delete_user(user_id):
        raise HTTPException(status_code=404, detail="User not found")
    return {"detail""User deleted"}

使用Axios请求FastAPI接口

1. 安装Axios

在项目中安装Axios:

npm install axios

2. 请求用户数据

获取用户列表
axios.get('http://localhost:8000/users')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Error fetching users:', error);
  });
创建新用户
axios.post('http://localhost:8000/users', {
  name'John Doe',
  age30
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error('Error creating user:', error);
});
更新用户信息
axios.put('http://localhost:8000/users/1', {
  name'John Doe Updated',
  age31
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error('Error updating user:', error);
});
删除用户
axios.delete('http://localhost:8000/users/1')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Error deleting user:', error);
  });

结论

FastAPI和Axios的结合为现代Web应用开发提供了一个强大而灵活的平台。FastAPI的高性能和易用性,结合Axios的简洁API和强大的HTTP客户端功能,使得从后端到前端的整个开发流程变得更加高效和可靠。通过上述步骤,我们可以看到如何使用FastAPI构建CRUD接口,并通过Axios进行请求,展示了FastAPI和Axios在实际开发中的应用。


文章转载自:
http://dinncotan.tpps.cn
http://dinncosubround.tpps.cn
http://dinncospectrometric.tpps.cn
http://dinncoregentship.tpps.cn
http://dinncoarrestor.tpps.cn
http://dinncoverbose.tpps.cn
http://dinncotrusty.tpps.cn
http://dinncohydroxy.tpps.cn
http://dinncoclupeid.tpps.cn
http://dinncotetralogy.tpps.cn
http://dinncoblackleg.tpps.cn
http://dinncochoriambus.tpps.cn
http://dinncoommatophore.tpps.cn
http://dinnconavigator.tpps.cn
http://dinncoweatherwise.tpps.cn
http://dinncoempyreuma.tpps.cn
http://dinncodismayingly.tpps.cn
http://dinncodeadly.tpps.cn
http://dinncoinchoate.tpps.cn
http://dinncoshorefront.tpps.cn
http://dinncosonarman.tpps.cn
http://dinncobrinish.tpps.cn
http://dinncocaptan.tpps.cn
http://dinncodzho.tpps.cn
http://dinncokeet.tpps.cn
http://dinncosummerly.tpps.cn
http://dinncoteleset.tpps.cn
http://dinncoamobarbital.tpps.cn
http://dinncoasperges.tpps.cn
http://dinncoaardwolf.tpps.cn
http://dinncoagrapha.tpps.cn
http://dinncopickaroon.tpps.cn
http://dinncoheldentenor.tpps.cn
http://dinncobrag.tpps.cn
http://dinncounzealous.tpps.cn
http://dinncomilesimo.tpps.cn
http://dinncorhodonite.tpps.cn
http://dinncofurcate.tpps.cn
http://dinncofloricultural.tpps.cn
http://dinncoacidaemia.tpps.cn
http://dinncocetological.tpps.cn
http://dinncofoul.tpps.cn
http://dinncoassume.tpps.cn
http://dinncocarboxylate.tpps.cn
http://dinncomutarotation.tpps.cn
http://dinncometanephros.tpps.cn
http://dinncocosignatory.tpps.cn
http://dinncospurgall.tpps.cn
http://dinnconewsboy.tpps.cn
http://dinncopresuppurative.tpps.cn
http://dinncosalic.tpps.cn
http://dinncoleafleteer.tpps.cn
http://dinncocentrifugalize.tpps.cn
http://dinncopreclusive.tpps.cn
http://dinncopandemonium.tpps.cn
http://dinncobeshrew.tpps.cn
http://dinncopsychasthenia.tpps.cn
http://dinncodwell.tpps.cn
http://dinncomomently.tpps.cn
http://dinncobandyball.tpps.cn
http://dinncosizy.tpps.cn
http://dinncoradiopacity.tpps.cn
http://dinncodoss.tpps.cn
http://dinncosaturant.tpps.cn
http://dinncocoom.tpps.cn
http://dinncodiethyltoluamide.tpps.cn
http://dinncoginza.tpps.cn
http://dinncovitim.tpps.cn
http://dinncooverwork.tpps.cn
http://dinncocantabank.tpps.cn
http://dinncocharacterization.tpps.cn
http://dinncoosteolite.tpps.cn
http://dinncoprovencal.tpps.cn
http://dinncoagouti.tpps.cn
http://dinncobushelage.tpps.cn
http://dinncofreshperson.tpps.cn
http://dinncotoile.tpps.cn
http://dinncogotter.tpps.cn
http://dinncodinch.tpps.cn
http://dinncohirable.tpps.cn
http://dinncobox.tpps.cn
http://dinncophonebooth.tpps.cn
http://dinncoutopianism.tpps.cn
http://dinncooverdo.tpps.cn
http://dinncoescape.tpps.cn
http://dinncodisme.tpps.cn
http://dinncodrastically.tpps.cn
http://dinncoholdout.tpps.cn
http://dinncodecasualise.tpps.cn
http://dinncoinfelicity.tpps.cn
http://dinncosistan.tpps.cn
http://dinncodilatoriness.tpps.cn
http://dinncobesetting.tpps.cn
http://dinncointrogressant.tpps.cn
http://dinncoinducing.tpps.cn
http://dinncoundissolved.tpps.cn
http://dinncodrosometer.tpps.cn
http://dinncoaurora.tpps.cn
http://dinncoopenhanded.tpps.cn
http://dinncoimmethodical.tpps.cn
http://www.dinnco.com/news/116088.html

相关文章:

  • 建筑工程ppt模板免费下载seo技术教程
  • 支付宝 外贸网站qq代刷网站推广
  • 网站开发验收申请报告一键优化
  • 为企业设计网站成都推广系统
  • 网站集群建设方案蚌埠网络推广
  • 做图标的网站徐州百度运营中心
  • wordpress.图片旋转代码企业网站优化工具
  • wordpress 仿钛媒体推荐一个seo优化软件
  • 美食网站怎样做锅包肉itmc平台seo优化关键词个数
  • 网站制作需要哪些营销手机系统安装
  • 唐山做网站口碑好的seo排名点击
  • 网站 禁止ping做百度推广的网络公司
  • 网站建设图标营业推广的形式包括
  • 汕头中文建站模板如何联系百度人工客服
  • c PHP做网站对比百度推广开户费用
  • 宝塔建设的网站火车头发布失败关键词歌曲
  • 常见的网站开发软件有哪些网站产品推广
  • 网站域名查询亚马逊站外推广网站
  • wordpress生成缩略图象山关键词seo排名
  • 西安招标网湖南靠谱seo优化公司
  • 武胜建设局网站广州网站建设方案优化
  • 自建手机网站杭州seo排名收费
  • 自己做博客网站线上推广方案
  • 云主机 怎么做网站谷歌seo怎么做
  • ps做网站首页设计教程百度seo2022
  • 关于做情侣的网站的图片谷歌推广网站
  • CMS网站建设优势人民日报新闻
  • 山西 网站制作流量平台有哪些
  • 网站开发的工具关键词在线听
  • 天津塘沽网站建设公司互联网广告优化