FastAPI 프로젝트 진행(완료 버튼 작동, RedirectResponse) - 89

2023. 1. 29. 17:20·Develop/FastAPI
728x90
SMALL

1. 새로운 API 생성

@router.get("/complete/{todo_id}", response_class=HTMLResponse)
async def complete_todo(request: Request, todo_id: int, db: Session = Depends(get_db)):
    todo = db.query(models.Todos).filter(models.Todos.id == todo_id).first()

    todo.complete = not todo.complete

    db.add(todo)
    db.flush()
    db.commit()

    return RedirectResponse(url="/todos", status_code=status.HTTP_302_FOUND)
RedirectResponse - complete_todo 호출 이후 todos로 이동
not todo.complete - complete의 type은 boolean으로 False값이 오면 True로 True값이 오면 False로 바꾼다.

 

2. home.html 수정

<tbody>
    {% for todo in todos %}
    {% if todo.complete == False %}

	<tr class="pointer">
        <td>{{loop.index}}</td>
       <td>{{todo.title}}</td>
        <td>
           <button onclick="window.location.href='complete/{{todo.id}}'"
                   type="button" class="btn btn-success">Complete</button>
           <button onclick="window.location.href='edit-todo/{{todo.id}}'"
                   type="button" class="btn btn-info">Edit
           </button>
        </td>
    </tr>

    {% else %}
    <tr class="pointer alert alert-success">
        <td>{{loop.index}}</td>
       <td class="strike-through-td">{{todo.title}}</td>
        <td>
           <button onclick="window.location.href='complete/{{todo.id}}'"
                   type="button" class="btn btn-success">Undo</button>
           <button onclick="window.location.href='edit-todo/{{todo.id}}'"
                   type="button" class="btn btn-info">Edit
           </button>
        </td>
    </tr>

    {% endif %}
    {% endfor %}
</tbody>
{% for xx in xx%} - python for문
{% if %} - python if문
{% else %} - python else문

Complete 버튼을 누르면 False값이 True로 업데이트 되면서 else 구문으로 넘어간다.
Undo 버튼을 누르면 True값이 False로 업데이트 되면서 if 구문으로 넘어간다.
else구문의 td class는 static의 base.css에 값이 설정되어 있다. - 글자에 라인선이 그어짐
else구문의 tr class는 static의 bootstrap.css에 값이 설정되어있다. - 상자 전체에 초록색이 채워짐.

 

3. 웹 확인

 

728x90
SMALL
저작자표시 비영리 변경금지

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

FastAPI 프로젝트 진행(로그인 추가, 쿠키 세션) - 91  (0) 2023.01.29
FastAPI 프로젝트 진행(로그인 기능 구현 API) - 90  (0) 2023.01.29
FastAPI 프로젝트 진행(데이터 삭제 API, RedirectResponse) - 88  (0) 2023.01.29
FastAPI 프로젝트 진행(데이터 수정 API, RedirectResponse) - 87  (0) 2023.01.29
FastAPI 프로젝트 진행(DB 데이터 화면 뿌리기) -86  (0) 2023.01.29
'Develop/FastAPI' 카테고리의 다른 글
  • FastAPI 프로젝트 진행(로그인 추가, 쿠키 세션) - 91
  • FastAPI 프로젝트 진행(로그인 기능 구현 API) - 90
  • FastAPI 프로젝트 진행(데이터 삭제 API, RedirectResponse) - 88
  • FastAPI 프로젝트 진행(데이터 수정 API, RedirectResponse) - 87
동석해요
동석해요
공부하고 싶은게 많은, 사소한 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 프로젝트 진행(완료 버튼 작동, RedirectResponse) - 89
상단으로

티스토리툴바