질문 상세 템플릿에 오류 표시 영역 추가하기가 안되내요ㅠㅠ
답변 내용없이 답변 등록시 오류 메시지 뜨게하는게 안되내요 ㅠㅠ 책 내용과 같이 코딩했는데 왜그럴까요 ㅠㅠ
<form action="{% url 'product:comment_create' game.id %}" method="post" class="my-3">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger " role="alert">
{% for field in form %}
{% if field.errors %}
<strong>{{ field.label }}</strong>
{{ field.errors }}
{% endif %}
{% endfor %}
</div>
{% endif %}
<div class="form-group">
<textarea name="content" id="content" class="form-control" rows="10"></textarea>
</div>
<input class="btn btn-secondary" type="submit" value="등록">
</form>
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['content']
labels = {
'content': '리뷰',
}
def comment_create(request, game_id):
"""
product(game) 댓글 등록
"""
game = get_object_or_404(Game, pk=game_id)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.posted_date = timezone.now()
comment.game = game
comment.save()
return redirect('product:detail', game_id=game.id)
else:
form = CommentForm()
context = {'game': game, 'form': form}
return render(request, 'product/game_detail.html', context)
class Comment(models.Model):
game = models.ForeignKey(Game, on_delete=models.CASCADE)
content = models.TextField()
posted_date = models.DateTimeField()
123 님 516
M 2021년 3월 9일 2:31 오후