Develop/Django(Project)
1. TodoApp 만들기 _ 전체조회 템플릿 만들기
보보트레인
2023. 9. 19. 16:20
세팅은 Django(exercise) 글들 참고해주세요
Django.restAPI (with python/) _DRF 프로젝트 세팅 (tistory.com)
Todo앱 목표
- CRUD 기능 탑재
- Bootstrap 사용하여 더 멋진 템플릿 구현
※ 작업순서는 템플릿 > 뷰 > url 순서로 진행
1. Todo 전체 조회 템플릿 만들기
templates/todo/todo_list.html 파일 생성
<html>
<head>
<title>TODO 목록 앱</title>
<link
rel="stylesheet"
/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icon@1.7.1/font/boot-strap-icons.css">
</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')),
]
전체 조회 기능이 구현 완료되었다!
<결과화면>
반응형