def comment_edit(request, song_id, comment_id=None): """コメントの編集""" song = get_object_or_404(Song, pk=song_id) # 親の曲を読む # comment_idが指定されている(修正時) if comment_id: comment = get_object_or_404(Comment, pk=comment_id) # comment_idが指定されていない(追加時) else: comment = Comment() if request.method == 'POST': # POSTされたrequestデータからフォームを作成 form = CommentForm(request.POST, instance=comment) if form.is_valid(): comment = form.save(commit=False) comment.song = song comment.save() return redirect('myapp:comment_list', song_id=song_id) else: # GETの時 form = CommentForm(instance=comment) return render(request, 'myapp/comment_edit.html', dict(form=form, song_id=song_id, comment_id=comment_id))