Djangoフォームの検証に失敗すると、フォームが失敗したページに自動的に戻りますが、ページのhtmlアンカーに移動したいと思います。これが私が持っているコードです:
def detail(request, post_id):
p = get_object_or_404(Post, pk=post_id)
# get all the comments for the post
comments = p.comment_set.all().order_by('pub_date')
# to add a comment to the post
if request.method=='POST':
form = CommentForm(request.POST)
if form.is_valid():
c = Comment()
c.body = request.POST['body']
c.post = p
c.save()
return HttpResponseRedirect(reverse('journal.views.detail', kwargs={'post_id': post_id}) + "#comment-" + str(c.id))
else:
form = CommentForm()
return render(request, 'posts/detail.html', {'post': p, 'comments': comments, 'form': form})
フォームは、ブログ投稿の下部にあるコメントフォームです。フォームが無効な場合は、ページの下部にあるコメントフォームのhtmlアンカーにジャンプします。現在、ページの上部に移動し、下にスクロールしてエラーがあることを確認する必要があります。
うまくいかなかったいくつかのことを試しました。助けてください!