Tags
- count
- 통계학
- 트리
- Vue
- outer join
- regexp
- 큐
- drf
- ORM
- Queue
- SQL
- Django
- 그리디
- migrations
- distinct
- 백트래킹
- 스택
- Article & User
- 완전검색
- update
- 쟝고
- stack
- M:N
- N:1
- 이진트리
- DB
- delete
- 뷰
- create
- Tree
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Notice
Recent Posts
Link
데이터 분석 기술 블로그
Django에 대하여(16)_ORM with view(Delete와 Update) 본문
1. Delete
1.1 Delete 기능 구현
# articles/urls.py
urlpatterns = [
...
path('<int:pk>/delete/', views.delete, name='delete')
]
# articles/views.py
def delete(request, pk):
article = Article.objects.get(pk=pk)
article.delete()
return redirect('articles:index')
<!-- articles/detail.html -->
<body>
<h2>Detail</h2>
...
<hr>
<form action="{% url 'articles:delete' article.pk %}" method="POST">
{% csrf_token %}
<input type="submit" value="DELETE">
</form>
<a href="{% url 'articles:index' %}">[back]</a>
</body>
2. Update
Update 로직을 구현하기 위해 필요한 view 함수의 개수는 몇 개일까요? 사용자 입력 데이터를 받을 페이지를 렌더링 하는 edit과 사용자가 입력한 데이터를 받아 DB에 저장하는 update, 이렇게 두 개가 있습니다.
2.1 edit 기능 구현
# articles/urls.py
urlpatterns = [
...
path('<int:pk>/edit/', views.edit, name='edit')
]
# articles/views.py
def edit(request, pk):
article = Article.objects.get(pk=pk)
context = {
'article': article,
}
return render(request, 'articles/edit.html', context)
수정 시 이전 데이터가 출력될 수 있도록 작성한다.
<!-- articles/edit.html -->
<h1>EDIT</h1>
<form action="#" method="POST">
{% csrf_token %}
<div>
<label for="title">Title: </label>
<input type="text" name="title" id="title value="{{ aritcle.title }}">
</div>
<div>
<label for "content">Content: </label>
<textarea name="content" id="content">{{ article.content }}</textarea>
</div>
<input type="submit">
</form>
<hr>
<a href="{% url 'articles:index' %}">[back]</a>
edit 페이지로 이동하기 위한 하이퍼링크 작성
<!-- articles/detail.html -->
<body>
<a href="{% url 'articles:edit' article.pk %}">EDIT</a><br>
<form action="{% url 'articles:delete' article.pk %}" method="POST">
{% csrf_token %}
<input type="submit" value="DELETE">
</form>
<a href="{% url 'articles:index' %}">[back]</a>
</body>
2.2 update 기능 구현
# articles/urls.py
urlpatterns = [
...
path('<int:pk>/update/', views.update, name='update')
]
# articles/views.py
def update(request, pk):
article = Article.objects.get(pk=pk)
article.title = request.POST.get('title')
article.content = request.POST.get('content')
article.save()
return redirect('articles:detail', article.pk)
작성 후 게시글 수정 테스트
<!-- articles/edit.html -->
<h1>EDIT</h1>
<form action="{% url 'articles:update' article.pk %}" method="POST">
{% csrf_token %}
<div>
<label for="title">Title: </label>
<input type="text" name="title" id="title value="{{ aritcle.title }}">
</div>
<div>
<label for "content">Content: </label>
<textarea name="content" id="content">{{ article.content }}</textarea>
</div>
<input type="submit">
</form>
<hr>
<a href="{% url 'articles:index' %}">[back]</a>
'백엔드' 카테고리의 다른 글
Django에 대하여(17)_Static files (0) | 2024.04.08 |
---|---|
Django에 대하여(17)_HTTP request methods와 redirect (0) | 2024.04.07 |
Django에 대하여(15)_ORM with view(Create) (0) | 2024.04.05 |
Django에 대하여(14)_ORM with view(Read) (0) | 2024.04.04 |
Django에 대하여(13)_QuerySet API 실습 (0) | 2024.04.03 |