Tags
- Tree
- DB
- stack
- M:N
- migrations
- 쟝고
- regexp
- 그리디
- count
- 큐
- Vue
- 뷰
- ORM
- drf
- outer join
- 이진트리
- Django
- 스택
- 트리
- N:1
- update
- delete
- SQL
- 완전검색
- 백트래킹
- create
- Article & User
- 통계학
- distinct
- Queue
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
데이터 분석 기술 블로그
DB에 대하여(9)_댓글 CREATE & READ (feat. Django) 본문
1. 댓글 CREATE
댓글 작성 시 이전에 게시글 작성할 때와 동일한 에러가 발생합니다.
댓글의 user_id 필드 데이터가 누락되었기 때문입니다.
댓글 작성 시 작성자 정보가 함께 저장할 수 있도록 작성합니다.
# articles/views.py
def comments_create(request, pk):
article = Article.objects.get(pk=pk)
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
comment = comment_form.save(commit=False)
comment.article = article
comment.user = request.user
comment_form.save()
return redirect('articles:detail', article.pk)
...
댓글 작성 후 테이블을 확인합니다.
2. 댓글 READ
댓글 출력 시 댓글 작성자와 함께 출력합니다.
<!-- articles/detail.html -->
{% for comment in comments %}
<li>
{{ comment.user }} - {{ comment.content }}
...
</li>
{% endfor %}
'DB' 카테고리의 다른 글
DB에 대하여(11)_Many to many relationships (feat. Django) (0) | 2024.04.28 |
---|---|
DB에 대하여(10)_댓글 DELETE (feat. Django) (0) | 2024.04.27 |
DB에 대하여(8)_Comment & User 모델 관계 설정 (feat. Django) (0) | 2024.04.25 |
DB에 대하여(7)_Article & User 게시글 READ & UPDATE & DELETE (feat. Django) (0) | 2024.04.24 |
DB에 대하여(7)_Article & User 게시글 CREATE (feat. Django) (0) | 2024.04.23 |