Develop/Django(Project)

1. TodoApp 만들기 _ 전체조회 템플릿 만들기

보보트레인 2023. 9. 19. 16:20

세팅은 Django(exercise) 글들 참고해주세요

Django.restAPI (with python/) _DRF 프로젝트 세팅 (tistory.com)

 

Django.restAPI (with python/) _DRF 프로젝트 세팅

목표 : 파이썬 + Django를 이용하여 restful한 api를 만들어보고 나만의 todo app 개발 ( +@ DRF개념을 이해하고 응용해보자. ) 운영체제 : windows64bit 파이썬 버전 : 3.11.5 ( 최소 3.8.0 이상) Django 버전 : 3.2.10 DR

iron-mentalman.tistory.com

 

Todo앱 목표

  • CRUD 기능 탑재
  • Bootstrap 사용하여 더 멋진 템플릿 구현

※ 작업순서는 템플릿 > 뷰 > url 순서로 진행


1. Todo 전체 조회 템플릿 만들기

templates/todo/todo_list.html 파일 생성

<html>
    <head>
        <title>TODO 목록 앱</title>
        <link
            rel="stylesheet"
        />
    </head>
    <body>
        <div class="container">
            <h1>TODO 목록 앱</h1>
            <p>
                <a href=""><i class="bi-plus"></i>Add Todo</a>
                <a href="" class="btn btn-primary" style="float:right">완료한 TODO 목록</a>
            </p>
            <ul class="list-group">
                {% for todo in todos %}
                <li class="list-group-item">
                    <a href="">{{ todo.title }}</a>
                    {% if todo.important %}
                        <span class="badge badge-danger">!</span>
                    {% endif %}
                    <div style="float:right">
                        <a href="" class="btn btn-danger">완료</a>
                        <a href="" class="btn btn-outline-primary">수정하기</a>
                    </div>    
                </li>
                {% endfor %}
            </ul>
        </div>
    </body>
</html>

2. Todo 전체 조회 뷰 만들기

todo/views.py의 코드를 다음과 같이 작성

from django.shortcuts import render, redirect
from .models import Todo

# Create your views here.
def todo_list(request):
    todos = Todo.objects.filter(complete=False)
    return render(request, 'todo/todo_list.html', {'todos': todos})

3. Todo  전체 조회 URL 연결하기

todo/urls.py의 코드를 다음과 같이 작성

from django.urls import path
from . import views

urlpatterns = [
    path('', views.todo_list, name='todo_list'),
]

전체 앱에도 연결해야함. 따라서 mytodo.urls.py에 다음과 같이 연결

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('todo/', include('todo.urls')),
    path('', include('todo.urls')),
]

전체 조회 기능이 구현 완료되었다!

 

<결과화면>

반응형