개발로 자기계발
article thumbnail
728x90

PostgreSQL user 테이블
PostgreSQL todos 테이블

 

1. 새로운 API 생성

@router.get("/delete/{todo_id}")
async def delete_todo(request: Request, todo_id: int, db: Session = Depends(get_db)):

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

    if todo_model is None:
        return RedirectResponse(url="/todos", status_code=status.HTTP_302_FOUND)

    db.query(models.Todos).filter(models.Todos.id == todo_id).delete()

    db.commit()

    return RedirectResponse(url="/todos", status_code=status.HTTP_302_FOUND)

 

2. edit.html 수정

{% 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">
               <!--제출버튼-->
                <button type="submit" class="btn btn-primary">Edit your todo</button>
               <button onclick="window.location.href='/todos/delete/{{todo.id}}'"
                       type="button" class="btn btn-danger"> Delete</button>
            </form>
        </div>
    </div>
</div>
onclick - 클릭 시 delete API 호출

 

3. 웹 확인

728x90
SMALL
profile

개발로 자기계발

@김잠봉

틀린부분이나 조언이 있다면 언제든 환영입니다:-)