(이전 기록과 이어지는 내용임)
- templates/todo_form.html 생성
<!-- 중략 -->
<body>
<div id = 'app'>
<h1>My Todo App !</h1>
<strong>서로 할 일이나 의견을 공유해 봅시다.</strong>
<br>
<!-- form을 보여주는 부분만 남겨둠 -->
<!-- form태그에는 action과 method속성이 필요함 -->
<form class="inputBox" action="." method="POST"> {% csrf_token %} <!--csrf공격 방지 위한 토큰-->
<input class="name" type="text" placeholder="name ..." name="name">
<input class="item" type="text" placeholder="type anyting welcomed ..."
name="todo"> <!-- 엔터 눌러도 add기능 되도록 구현 -->
<!-- v-model대신 name속성으로 변경 = 변수명은 todoTable의 변수명과 동일 해야함(주의!) -->
<button type="submit">ADD</button>
</form>
</div>
</body>
</html>
- templates/todo_list.html 생성
<body>
<div id = 'app'>
<h1>My Todo App !</h1>
<strong>서로 할 일이나 의견을 공유해 봅시다.</strong>
<br>
<!-- todoList.html에서는 form부분을 지우고 list부분만 만들어줌 -->
<!-- vue.js코드는 삭제하고 html태그와 장고 템플릿 문법으로 변경-->
<ul class="todoList">
<!-- v-for는 장고 템플릿 문법으로 대체 -->
{% for todo in object_list %} <!-- listView에서는 object_list라는 context 변수를 넘겨줌 -->
<li>
<!-- vue.js {mustache문법} -> { { 장고 템플릿 문법 } } 으로 변경 -->
<span>{{ todo.name }} :: {{ todo.todo }}</span> <!-- column명은 todo임!-->
<!-- 삭제버튼 클릭했을 때의 동작 <a>태그로 넣어줌 : delete url요청 & 파라미터로 todoID 넣음 -->
<span class="removeBtn"><a href="{% url 'todo:delete' todo.id %}">×</a></span>
</li>
{% endfor %}
</ul>
</div>
</body>
{}
은 장고 템플릿 문법{{ }}
으로 대체됨
- templates/todo_confirm_delete.html 생성
<body>
<div id = 'app'>
<h1>Todo Delete</h1>
<!-- deleteView에서는 context변수 obejct를 넘겨줌 -->
<p>Are you sure to delete {{ obeject }} ?</p>
<br>
<form action="." method="POST"> {% csrf_token %}
<button type="submit">Confirm</button>
</form>
</div>
</body>
- 이름을 입력하지 않으면 '홍길동'이라는 default값 넣어주는 기능 추가
# models.py
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
if not self.name:
self.name = '홍길동'
super().save()