FastAPI 프로젝트 진행(데이터 수정 API, RedirectResponse) - 87

2023. 1. 29. 16:42·Develop/FastAPI
728x90
SMALL

PostgreSQL user 테이블
PostgreSQL todos 테이블

 

1. 데이터 수정 API 생성

@router.post("/edit-todo/{todo_id}", response_class=HTMLResponse)
async def edit_todo_commit(request: Request, todo_id: int, title: str = Form(...),
                           description = Form(...), priority: int = Form(...),
                           db: Session = Depends(get_db)):

    todo_model = db.query(models.Todos).filter(models.Todos.id == todo_id).first()

    todo_model.title = title
    todo_model.description = description
    todo_model.priority = priority

    db.add(todo_model)
    db.flush()
    db.commit()

    return RedirectResponse(url="/todos", status_code=status.HTTP_302_FOUND)
RedirectResponse - edit_todo_commit 호출 이후 - url todos로 넘어감

 

2. html form method POST로 변경

{% include 'layout.html' %}

<div class="container">
    <div class="card">
       <!--상단 타이틀-->
        <div class="card-header">
            Let's edit your todo!
        </div>
       <!--줄바꿈-->
        <div class="card-body">
            <form method="POST"...>
            </form>
        </div>
    </div>
</div>
@router.get("/edit-todo/{todo_id}", response_class=HTMLResponse)
async def edit_todo(request: Request, todo_id: int, db: Session = Depends(get_db)):

    todo = db.query(models.Todos).filter(models.Todos.id == todo_id).first()

    return templates.TemplateResponse("edit-todo.html", {"request":request, "todo": todo})

@router.post("/edit-todo/{todo_id}", response_class=HTMLResponse)
async def edit_todo_commit(request: Request, todo_id: int, title: str = Form(...),
                           description = Form(...), priority: int = Form(...),
                           db: Session = Depends(get_db)):

    todo_model = db.query(models.Todos).filter(models.Todos.id == todo_id).first()

    todo_model.title = title
    todo_model.description = description
    todo_model.priority = priority

    db.add(todo_model)
    db.flush()
    db.commit()

    return RedirectResponse(url="/todos", status_code=status.HTTP_302_FOUND)
url주소가 같아도 method 방법이 다르므로 상관없음

 

3. 웹 확인

Cut the grass의 title이 변경된 모습

 

Cut the grass!!! 의 priority가 5에서 >> 3으로 변경된 모습
728x90
SMALL
저작자표시 비영리 변경금지

'Develop > FastAPI' 카테고리의 다른 글

FastAPI 프로젝트 진행(완료 버튼 작동, RedirectResponse) - 89  (0) 2023.01.29
FastAPI 프로젝트 진행(데이터 삭제 API, RedirectResponse) - 88  (0) 2023.01.29
FastAPI 프로젝트 진행(DB 데이터 화면 뿌리기) -86  (0) 2023.01.29
FastAPI 프로젝트 진행(데이터 생성 API, RedirectResponse) - 85  (0) 2023.01.29
FastAPI 프로젝트 진행(HTML 렌더링, DB 연결)- 84  (0) 2023.01.29
'Develop/FastAPI' 카테고리의 다른 글
  • FastAPI 프로젝트 진행(완료 버튼 작동, RedirectResponse) - 89
  • FastAPI 프로젝트 진행(데이터 삭제 API, RedirectResponse) - 88
  • FastAPI 프로젝트 진행(DB 데이터 화면 뿌리기) -86
  • FastAPI 프로젝트 진행(데이터 생성 API, RedirectResponse) - 85
동석해요
동석해요
공부하고 싶은게 많은, 사소한 IT 지식들 공유
    250x250
  • 동석해요
    개발로 자기계발
    동석해요
  • 전체
    오늘
    어제
    • 분류 전체보기 (226)
      • Develop (126)
        • 기초지식 (12)
        • FastAPI (102)
        • Django (11)
      • Database & Data (62)
        • 기초지식 (16)
        • MySQL (29)
        • PostgreSQL (8)
        • 데이터 분석 (9)
      • 인공지능 (11)
        • PyTorch (9)
      • Cloud (8)
        • AWS (4)
        • GCP (2)
      • 버그처리 (14)
      • 회고 & 일상 (5)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
동석해요
FastAPI 프로젝트 진행(데이터 수정 API, RedirectResponse) - 87
상단으로

티스토리툴바